Python Forum
data input - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: data input (/thread-26603.html)



data input - chpyel - May-06-2020

how I control the data input from user input error


RE: data input - Larz60+ - May-06-2020

an example or elaboration would be useful.


RE: data input - chpyel - May-06-2020

a=int(input())
b=int(input())
c=a/b


ValueError and ZeroDivisionError and after continue program


RE: data input - SheeppOSU - May-06-2020

Whenever you want to grab an integer value from an input, it's best to do a check and make sure that they've entered in an integer. For this you can use a try and except statement. Here's a code example.
gettingInput = True
integer = None

while gettingInput: 
    try:
        integer = int(input("Type an integer: ")) #Will go to except if an error occurs, in other words, they didn't enter something that can be properly turned into an integer.
        gettingInput = False #Will exit out of while loop, you can also get rid of this variable and use a break if you like.
    except:
        print("That's not an integer")

print("You chose %s as your integer" %integer)
You could also use a try and except statement to make sure that there are no errors when someone enters something that cannot divide properly.
Hope this helps.


RE: data input - chpyel - May-06-2020

nine-line codes for controlling an input
and other nine for another input ?


RE: data input - SheeppOSU - May-06-2020

(May-06-2020, 10:01 PM)chpyel Wrote: and other nine for another input ?
If you need to do this over and over, create it into a function. Functions are for repeated chunks of code, splitting logic, and organizing code. So if you need to collect input multiple times then you can make it into a function. Your code might look something like this
def get_input(message):
    #Insert code that I wrote earlier

def main():
    integer1 = get_input("Type your first integer: ")
    integer2 = get_input("Type your second integer: ")
    integer3 = get_input("Type your third integer: ")
    integer4 = get_input("Type your fourth integer: ")
    #Do something with them

if __name__ == "__main__": #If this code is the main running code (not being imported)
    main()
You could simplify it further with a dictionary
def get_input(message):
    #Insert code that I wrote earlier

def main():
    intDict = {}
    numberOfIntegers = get_input("How many integers do you want? ")
    for num in range(0, numberOfIntegers):
        intDict.update({"Integer %s" %(num+1): get_input("Type integer number %s: " %(num+1))})
    #Do something with the dictionary of integers

if __name__ == "__main__":
    main()
This way you could get up to 100 integers efficiently. You can even get the user to separate it into commas and then use string.split() where string would be the input.
Hope this helps.


RE: data input - jefsummers - May-06-2020

The length of code was trying to explain more to you. If brevity is important
try:
    print(int(input("Enter an integer"))/int(input("Enter another integer")))
except:
    print("Either you entered a non-numeric value or tried to divide by zero")



RE: data input - chpyel - May-06-2020

while True:
try:
a=int(input())
except:
print ('invalid value')
else:
break


how to put second input here ?

(May-06-2020, 10:31 PM)jefsummers Wrote: The length of code was trying to explain more to you. If brevity is important
try:
    print(int(input("Enter an integer"))/int(input("Enter another integer")))
except:
    print("Either you entered a non-numeric value or tried to divide by zero")



this code is small and does the two checks I wanted but does not return to the input

while True:
try:
print(int(input("Enter an integer"))/int(input("Enter another integer")))
except:
print("Either you entered a non-numeric value or tried to divide by zero")
else:
break


so is better but I have no values for variables a and b


RE: data input - perfringo - May-07-2020

(May-06-2020, 08:52 PM)chpyel Wrote: how I control the data input from user input error

You have answer to your initial question - use try...except.

How to implement is another story. You haven't given enough information to understand what you want to accomplish. As it smells like homework I would just generalise: one can have validation function which checks user input with try...except and returns only valid value. One can apply this function to whatever number of user inputs and assign result to whatever variable.