May-06-2020, 08:52 PM
how I control the data input from user input error
data input
|
May-06-2020, 08:52 PM
how I control the data input from user input error
May-06-2020, 09:20 PM
an example or elaboration would be useful.
May-06-2020, 09:27 PM
a=int(input())
b=int(input()) c=a/b ValueError and ZeroDivisionError and after continue program
May-06-2020, 09:42 PM
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.
May-06-2020, 10:01 PM
nine-line codes for controlling an input
and other nine for another input ? (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.
May-06-2020, 10:31 PM
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")
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 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
May-07-2020, 05:16 AM
(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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame. |
|