Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
data input
#1
how I control the data input from user input error
Reply
#2
an example or elaboration would be useful.
Reply
#3
a=int(input())
b=int(input())
c=a/b


ValueError and ZeroDivisionError and after continue program
Reply
#4
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.
Reply
#5
nine-line codes for controlling an input
and other nine for another input ?
Reply
#6
(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.
Reply
#7
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")
Reply
#8
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
Reply
#9
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 318 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  manually input data jpatierno 0 316 Nov-10-2023, 02:32 AM
Last Post: jpatierno
  Input network device connection info from data file edroche3rd 6 913 Oct-12-2023, 02:18 AM
Last Post: edroche3rd
Question in this code, I input Key_word, it can not find although all data was exact Help me! duchien04x4 3 972 Aug-31-2023, 05:36 PM
Last Post: deanhystad
  Showing an empty chart, then input data via function kgall89 0 943 Jun-02-2022, 01:53 AM
Last Post: kgall89
Question Change elements of array based on position of input data Cola_Reb 6 2,063 May-13-2022, 12:57 PM
Last Post: Cola_Reb
  input data validation plumberpy 2 1,746 Aug-11-2021, 12:04 PM
Last Post: plumberpy
  command line input (arg parse) and data exchange Simba 7 4,245 Dec-06-2019, 11:58 PM
Last Post: Simba
  user input to select and print data from another class python TyTheChosenOne 6 4,056 Aug-30-2018, 05:53 PM
Last Post: TyTheChosenOne
  Input Data machine Learning rafaelmoraes 2 3,261 Mar-15-2018, 01:24 PM
Last Post: rafaelmoraes

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020