I'm writing a Python function to collect user input and then return it as a dict.
The input should meet certain criteria, so I check for that and I ask the user to supply his input again if it doesn't check out.
This is my code:
def getIDs(stageid=0):
if stageid == 0:
stageid = input("Please supply stageID: ")
if (len(stageid) < 4 or len(stageid) > 5) or not stageid.isdigit():
print("Wrong stageID, try again.")
getIDs()
raceid = input("Please supply the raceID: ")
if (len(raceid) < 1 or len(raceid) > 3) or not raceid.isdigit():
print("Wrong raceID, try again.")
getIDs(stageid)
else:
# print(stageid)
return {"stageid": stageid, "raceid": raceid}
What I do is have the function just call itself if the stageid isn't correct, but I don't want the user to have to re-enter the stageid again if the raceid is incorrect, so if the raceid is incorrect, the I call the function with the stageid as a parameter.
My problem is, however, that if I get the stageid wrong several times, then get it right and continue to the raceid, it will ask for the raceid for as many times as I had entered the stageid. I can't figure out why. The commented print function prints all supplied stageid's in reverse order and in the end, the return value contains the initially supplied (wrong) stageid and the raceid.
Does anybody know why Python somehow loops through all values?
Vasilis
Just add a return statement when calling the recursive function so the termination will exit from that instance.
def getIDs(stageid=0):
if stageid == 0:
stageid = input("Please supply stageID: ")
if (len(stageid) < 4 or len(stageid) > 5) or not stageid.isdigit():
print("Wrong stageID, try again.")
return getIDs()
raceid = input("Please supply the raceID: ")
if (len(raceid) < 1 or len(raceid) > 3) or not raceid.isdigit():
print("Wrong raceID, try again.")
return getIDs(stageid)
else:
# print(stageid)
return {"stageid": stageid, "raceid": raceid}
Related
I want to print 5 attempts completed if i use all the attempts incorrectly
def user_name_repeat():
for repeat_user_name in range(4):
re_enter_user_name = input("Incorrect username, try again: ")
if re_enter_user_name == "vishesh":
print("Correct user name ur logged in")
break
input_ = input("enter user name: ")
if input_ == "vishesh":
print("Correct user id, you are logged in: ")
elif input_ != "vishesh":
print(user_name_repeat())
Why not wrap the whole thing inside a loop?
for _ in range(5):
input_ = input("enter user name: ")
if input_ == "vishesh":
print("Correct user id, you are logged in")
break
else:
print('Incorrect username, try again')
else:
print('Attempts exhausted')
More info on for/else
Edit: You could also fix up the logic in your own code with something like-
def user_name_repeat():
for repeat_user_name in range(4):
re_enter_user_name = input("Incorrect username, try again: ")
if re_enter_user_name == "vishesh":
print("Correct user name ur logged in")
return True
return False
input_ = input("enter user name: ")
if input_ == "vishesh":
print("Correct user id, you are logged in: ")
elif input_ != "vishesh":
if not user_name_repeat():
print('Attempts exhausted')
Really, all you need is to return an indicator (a boolean value in this case) from your function to let the caller know, whether the attempts succeeded.
Just use the else in your for loop, else will run after the loop is completed without encountering a break statement. Also, use return in your user_name_repeat() function, since you are calling it in print(user_name_repeat()) So it should return somethinig to the print() to print for:
def user_name_repeat():
for repeat_user_name in range(4):
re_enter_user_name = input("Incorrect username, try again: ")
if re_enter_user_name == "vishesh":
return("Correct user name ur logged in")
break
else:
return "5 attempts completed"
input_ = input("enter user name: ")
if input_ == "vishesh":
print("Correct user id, you are logged in: ")
elif input_ != "vishesh":
print(user_name_repeat())
Since you're working with a function, you should simply return the string instead of printing it inside of the function. That way, you can just return the "incorrect" string after the for loop.
def user_name_repeat():
for repeat_user_name in range(4):
re_enter_user_name = input("Incorrect username, try again: ")
if re_enter_user_name == "vishesh":
return "Correct user name ur logged in"
return "Number of incorrect tries exceeded limit"
Another approach would be using the else clause of the for loop. In loops, the code inside the else clause will only be executed if (and when) the loop ends "naturally"; that is - when the condition running the loop stops being valid (when the for reaches the end of the range(4), for example).
In your code, the loop only ends if the limit of incorrect attempts is reached, since you break the loop if the user inputs the right password. So the following could be done:
def user_name_repeat():
for repeat_user_name in range(4):
re_enter_user_name = input("Incorrect username, try again: ")
if re_enter_user_name == "vishesh":
print("Correct user name ur logged in")
break
else:
print("Number of incorrect tries exceeded limit")
However, I should point out that the first option is way better, specially considering you're printing the return of the function call later (and if the function has no explicit return, you're actually printing None).
You can even take the code out of the function, which should make things easier for you, as Chase's answer points out ;).
I have a problem in which users can input spaces or nothing and still pass through the program, how do I go about preventing this? I am still a beginner at python.
def orderFunction(): # The function which allows the customer to choose delivery or pickup
global deliveryPickup
deliveryPickup = input("Please input delivery or pickup: d for delivery p for pickup")
if deliveryPickup == "d":
global customerName
while True:
try:
customerName = (input("Please input your name"))
if customerName == (""):
print("Please input a valid name")
else:
break
global customerAddress
while True:
try:
customerAddress = (input("Please input your name"))
if customerAddress == (""):
print("Please input a valid Address")
else:
break
global customerPhnum
while True:
try:
customerPhnum = int(input("Please input your phone number"))
except ValueError:
print("Please input a valid phone number")
else:
break
print("There will also be a $3 delivery surcharge")
elif deliveryPickup == "p":
customerName = (input("Please input your name"))
if customerName == (""):
print("Please input a valid name")
orderFunction()
else:
print("Please ensure that you have chosen d for Delivery or p for Pickup")
orderFunction()
orderFunction()
Here is my attempt at doing this but I get all kinds of unindent and indent errors at the moment and I think my while loops are probably wrong.
Essentially if I input a space or hit enter into one of the customer inputs (customerName for instance) it gets stored. This needs to prevented and I have tried to fix it by using while loops which obviously haven't worked.
Hopefully someone has a solution to this problem
Many Thanks.
.strip() removes all tabs or spaces before and after a string.
Meaning all spaces == empty string. All tabs == empty string. So all you have to check if the length of that string != 0 or the string is not empty. Just use an infinite loop to keep on forcing the right input.
Also as a tip, you don't have to limit yourself into one function.
Here's a working code below.
def getNonBlankInput(message, error_message):
x = input(message)
while len(x.strip()) == 0:
x = input(error_message)
return x
def getValidIntegerInput(message, error_message):
msg = message
while(True):
try:
x = int(input(msg))
break
except ValueError:
msg = error_message
return x
def orderFunction(): # The function which allows the customer to choose delivery or pickup
global deliveryPickup
global customerName
global customerAddress
global customerPhnum
deliveryPickup = input("Please input delivery or pickup: d for delivery p for pickup")
if deliveryPickup == "d":
customerName = getNonBlankInput("Please input your name: ", "Please input a valid name: ")
customerAddress = getNonBlankInput("Please input your address: ", "Please input a valid address: ")
customerPhnum = getValidIntegerInput("Please input your phone number: ", "Please input a valid phone number: ")
print("There will also be a $3 delivery surcharge")
elif deliveryPickup == "p":
customerName = getNonBlankInput("Please input your name: ", "Please input a valid name: ")
else:
print("Please ensure that you have chosen d for Delivery or p for Pickup")
orderFunction()
orderFunction()
Try using a regular expression that checks if any character between "A-Z" has been inserted, if not, give an error
The while loops are a decent solution, you just need to add more checks to your if statements.
First, you don't need a try statement on the top two loops. Don't use a try statement unless you're expecting an error, which you need to handle with an except statement, like you do in the bottom while loop.
Then you just need to add more conditions to your top two loops, I don't know exactly what you want to prevent, but you could try checking the length of the input, also see this answer for an interesting method:
https://stackoverflow.com/a/2405300/8201979
Instead of using input right away you can make a function similar to this one that will only allow valid inputs.
You can use this valid_input function instead of input.
def valid_input(text):
not_valid = True
res = ''
while not_valid:
res = input(text)
if res.split(): # if text is empty or only spaces, this creates an empty list evaluated at False
not_valid = False
return res
here the check is pretty simple: every text made out of nothing or spaces won't be allowed and we will keep asking for the same input until a valid information is given.
I made this code simple just so you get a general idea. But you can change the validation test to your liking and maybe also output a warning saying why the input wasn't allowed so the person knows what to do. You can do more advanced validation with regex, and maybe you need a minimum text length etc...
You have indent error because you have a try statement without the corresponding except.
You need both to make it work (as you did in the Phone number section).
Here is a link to the try/except: docs
Also, you can check if a string is empty as detailed in this answer.
So for example you want to write:
try:
customerName = input("Please input your name")
if not customerName:
print("Please input a valid name")
else:
break
except ValueError:
print("Please input a valid name")
Although the above seems a bit redundant, so you might want to raise an exception if the customer name is empty, catch the exception in the except block, print the warning and return error (or something else).
try:
customerName = input("Please input your name")
if not customerName:
raise ValueError
except ValueError:
print("Please input a valid name")
else:
break
Try adding another while true for pick and delivery option so that it can prevent taking other inputs
you don't need any of those try/excepts (which are broken anyway).
Its difficult to figure out what you're trying to do, are you trying to raise an exception if an empty string is passed, or request another input from the user? You seem to be half implementing both at the moment.
If its the latter, something like this would work.
def func(fieldname):
while True:
val = input("Please input your {}".format(fieldname))
if val.strip() != "":
break
else:
print("Please input a valid {}".format(fieldname))
return val
delivery_pickup = input("Please input delivery or pickup: d for delivery p for pickup")
if delivery_pickup == "d":
customer_name = func("name")
address = func("address")
phone_number = func("phone number")
What you are looking for is the str.strip method that remove trailing whitespace in strings.
Also I think try is not particularly suited for your needs here.
customerName = input("Please input your name")
while not customerName.strip():
customerName = input("Please input a valid name")
for the phone number I would not convert to integer because if the phone number starts with zeros, they will not be stored.
How can I link the second info() call to a text file ?
print("Hi,welcome to the multiple choice quiz")
def info ():
print("Firstly we would like to collect some personal details:-?")
name = input("Please enter your first name?")
surname =input ("please enter your surname?")
email_address = input ("please enter your email addres #.co.uk")
username = input (" Chose a username?")
password = input ("Enter a Password?")
validation()
def validation():
correct = 0
while correct == 0:
correct=input(" is the following data is correct ?")
if correct in ["Y,y"]:
print("Well done you have registered for the quiz")
elif correct in ["N,n"]:
info()
else:
info()
You might want to drop your while loop.
info() calls your validation. "if" the data is correct, you finish, "else" you just call info() again.
That is pretty much what you did already. maybe you wanted it to look more like this:
print("Hi, welcome to the multiple choice quiz")
def info ():
print("Firstly we would like to collect some personal details:-?")
name = input("Please enter your first name?")
surname = input("please enter your surname?")
email_address = input("please enter your email address #.co.uk")
username = input(" Chose a username?")
password = input("Enter a Password?")
validation()
def validation():
correct = 0
correct = input(" is the data is correct ?")
if correct in ["Y,y"]:
print("Well done you have registered for the quiz")
elif correct in ["Y,y"]:
info()
else:
print "please type y or n"
validation()
info()
You wrote:
if correct in ["Y,y"]:
print("Well done you have registered for the quiz")
elif correct in ["N,n"]:
info()
else:
info()
Firstly, correct in ["Y,y"] will not do what you expect. ["Y,y"] is a list containing one element: "Y,y". That means correct in ["Y,y"] if and only if correct == "Y,y", and the user is not likely to enter that! You probably want a list with two elements: ["Y", "y"]. in can also test containment within strings, so you could use "Yy". You don't want a comma in there because then if the user enters just a comma that will pass the test as well, which is silly.
Putting that issue aside, if the user enters y or Y, it prints well done. If they enter n or N, info() is called. If they enter something else, info() is still called. That last part is surely not what you want: entering "N" and entering "A" should have different results! You want the user to be told that it's not valid. That part is easy, print a message. Then you want them to be asked again. Since they'll be asked for every iteration of your while loop, you just have to ensure that the loop continues. The loop will run as long as the condition correct == 0 is true, so just set correct to 0 and that will happen. So something like this:
else:
print("That's not valid")
correct = 0
OK, now to your question. You want to save those personal details to a file. You can't do that properly with the way your program is organised. The variables in info are local, so validation can't access them to save them. info has to do the saving (perhaps indirectly by passing the data along to another function). What we could do is have validation report the result of asking the user and let info decide what to do based on that:
def info ():
# collect data
if validation():
# save data
def validation():
correct = 0
while correct == 0:
correct=input(" is the following data is correct ?")
if correct in ["Y", "y"]:
print("Well done you have registered for the quiz")
return True
elif correct in ["N", "n"]:
return False
else:
print("That's not valid")
correct = 0
return will exit the validation function (thus ending the loop) and give the value returned to info to decide if the data should be saved.
Since the return statements end the loop, the only way the loop can continue is the else is reached, in which case explicitly making it continue with correct = 0 is silly. We can just make the loop go on forever until the function returns:
def validation():
while True:
correct=input(" is the following data is correct ?")
if correct in ["Y", "y"]:
print("Well done you have registered for the quiz")
return True
elif correct in ["N", "n"]:
info()
return False
else:
print("That's not valid")
I need help validating the input from the (random) questions I ask to make sure that the users input is only a number, rather than letters or any other random character. Also along with the validation there should be an error message notifying the user theyve done something wrong as well as repeat their chance to do the question.
So far the section of my code I need validating is the following:
def quiz():
x = random.randint(1, 10)
y = random.randint(1, 10)
ops = {'+': operator.add,'-': operator.sub,'*': operator.mul}
keys = list(ops.keys())
opt = random.choice(keys)
operation = ops[opt]
answer = operation(x, y)
print ("\nWhat is {} {} {}?".format(x, opt, y))
userAnswer= int(input("\nYour answer: "))
if userAnswer != answer: #validate users answer to correct answer
print ("\nIncorrect. The right answer is",answer,"")
print ("\n============= 8 =============")
return False
else:
print("\nCorrect!")
print ("\n============= 8 =============")
return True
for i in range(questions): #ask 10 questions
if quiz():
score +=1
print("\n{}: You got {}/{} questions correct.".format(name, score, questions,))
Thanks in advance!
Use try...except:
while True:
userAnswer = input("\nYour answer: ")
try:
val = float(userAnswer)
break
except ValueError:
print("That's not a number!")
If you want only integers, use int instead of float.
in Single line :
assert input('Enter Number: ').isdigit()
You may use function validate integer input:
def int_validation(user_Input):
try:
val = int(user_Input)
except ValueError:
print(user_Input,' not an integer!')
It would be good to make a function to accept your input. This can then keep prompting the user until a valid number is entered:
def get_number(prompt):
while True:
try:
return int(input(prompt))
except ValueError:
print("Please enter a number")
You can then change your input line as follows:
userAnswer = get_number("\nYour answer: ")
So I am making a prime number detector as a project. I’m VERY new to programming and my friend showed me a little python. I want to make a function that detects if the user puts in a number for the input (like 5,28,156,42,63) and if the put in something else (like banana,pants,or cereal) to give them a custom error saying "Invalid Number. Please Try Again" and then looping the program until they put in a number.
Please help me make this work.
def number_checker():
user_number = int(input('Please enter a Number: '))
check = isinstance(user_number, int)
if check == True:
print ('This is a number')
if check == False:
print ('This is not a number')
1) Casting input to int would raise an exception if the input string cannot be converted to int.
user_number = int(input('Please enter a Number: '))
^^^
2) It does not make sense to cross-verify user_number with int instance as it would already be int
3) You can try
def number_checker():
while not input("enter num: ").isdigit():
print("This is not a number")
print("This is a number")
number_checker()
Try following:
def number_checker():
msg = 'Put your number > '
while True:
user_input = input(msg)
correct = user_input.isdigit()
if correct:
print("This is an integer")
return # here you can put int(user_input)
else:
print("This is not an integer")
msg = 'You typed not integer. Try again.> '
if __name__ == '__main__':
number_checker()
And it's also good rule give names for your functions as verbs according to what they do. For this one, I would give, for example def int_input or something.