I have seen many answers to this question but am looking for something very specific. What I need to accomplish (in pseudo code) is this:
> FOR every ITEM in DICTIONARY, DO:
> PROMPT user for input
> IF input is integer
> SET unique-variable to user input
I'm very new to Python so the code may not be proper, but here is what I have:
def enter_quantity():
for q in menu:
quantities[q] = int(input("How many orders of " + str(q) + "?: "))
So this does everything but evaluate the user input. The problem I'm having is if the input is incorrect, I need to re-prompt them for the same item in the top-level for loop. So if it's asking "How many slices of pizza?" and the user inputs "ten", I want it to say "Sorry that's not a number" and return to the prompt again of "How many slices of pizza?".
Any/all ideas are appreciated. Thanks!
My final solution:
def enter_quantity():
for q in menu:
booltest = False
while booltest == False:
inp = input("How many orders of " + str(q) + "?: ")
try:
int(inp)
booltest = True
except ValueError:
print (inp + " is not a number. Please enter a nermic quantity.")
quantities[q] = int(inp)
You need a while loop with a try/except to verify the input:
def enter_quantity():
for q in menu:
while True:
inp = input("How many orders of {} ?: ".format(q))
try:
inp = int(inp) # try cast to int
break
except ValueError:
# if we get here user entered invalid input so print message and ask again
print("{} is not a number".format(inp))
continue
# out of while so inp is good, update dict
quantities[q] = inp
This bit of code is a little more useful if a menu is added otherwise it crashes at the first hurdle. I also added a dictionary to store the input values.
menu = 'pizza', 'pasta', 'vino'
quantities = {}
def enter_quantity():
for q in menu:
while True:
if q == 'pizza':
inp = input(f"How many slices of {q} ?: ")
elif q == 'pasta':
inp = input(f"How many plates of {q} ?: ")
elif q == 'vino':
inp = input(f"How many glasses of {q} ?: ")
try:
inp = int(inp) # try cast to int
break
except ValueError:
# exception is triggered if invalid input is entered. Print message and ask again
print("{} is not a number".format(inp))
continue
# while loop is OK, update the dictionary
quantities[q] = inp
print(quantities)
Then run the code from this command:
enter_quantity()
Related
def aboveIntegerInput(output_message="Enter your number: ", error_1="Please enter a number above {}!", error_2="Integers only!(Please do not leave this blank)", above=0):
while True:
try:
user_input = int(input(output_message))
if user_input >= above:
return int(user_input)
break
else:
print(error_1.format(above))
except ValueError:
print(error_2)
As you can see here the code is supposed to check if an input is an integer and it is above a certain value which by default is 0, but could be changed.
When the user inputs random letters and symbols it see that there is a value error and returns "Integers only!(Please do not leave this blank)".
I want to be able to check if the user inputs nothing, and in that case only it should output "This is blank/empty", the current way of dealing with this is to not check at all and just say "Integers only!(Please do not leave this blank)", in case there us a value error. I want to be able to be more specific and not just spit all the reasons at once. Can anyone please help me?
Thanks in advance.
You could do something like this :
def aboveIntegerInput(output_message="Enter your number: ", error_1="Please enter a number above {}!", error_2="Integers only!", above=0, error_3="Please do not leave this blank"):
while True:
user_input = input(output_message)
try:
user_input = int(user_input)
if user_input >= above:
return user_input
break
else:
print(error_1.format(above))
except ValueError:
if(not user_input):
print(error_3)
else:
print(error_2)
I moved the input outside the try/except block to be able to use it in the except ! This worked fine for me, I hope this is what you needed.
You could just break the input and the conversion to int into two steps, like this:
def aboveIntegerInput(output_message="Enter your number: ", error_1="Please enter a number above {}!", error_2="Integers only!(Please do not leave this blank)", above=0):
while True:
try:
user_input = input(output_message)
if not user_input:
print("Please do not leave this blank")
continue
user_input = int(user_input)
if user_input >= above:
return int(user_input)
break
else:
print(error_1.format(above))
except ValueError:
print(error_2)
This is my first small python task when i found out about speedtest-cli.
import speedtest
q = 1
while q == 1:
st = speedtest.Speedtest()
option = int(input("What do you want to test:\n 1)Download Speed\n 2)Upload Speed \n 3)Ping \n Please enter the number here: "))
if option == 1:
print(st.download())
q = int(input("Enter '1' if you want to continue or Enter '2' if you want to stop the test"))
elif option == 2:
print(st.upload())
q = int(input("Enter '1' if you want to continue or Enter '2' if you want to stop the test"))
elif option == 3:
servernames =[]
st.get_servers(servernames)
print(st.results.ping)
q = int(input("Enter '1' if you want to continue or Enter '2' if you want to stop the test"))
else:
print("Please enter the correct choice")
else:
print("Test is ended")
i am just a beginner so i could't find any way to shorten this code. Any tips would be helpful :)
If you don't care about execution time but only about code length:
import speedtest
q = 1
while q == 1:
st = speedtest.Speedtest()
st.get_servers([])
tst_results = [st.download(), st.upload(), st.results.ping]
option = int(input("What do you want to test:\n 1)Download Speed\n 2)Upload Speed \n 3)Ping \n Please enter the number here: "))
if option >= 1 and option <= 3:
print(tst_results[option-1])
q = int(input("Enter '1' if you want to continue or Enter '2' if you want to stop the test"))
else:
print("Please enter the correct choice")
else:
print("Test is ended")
Did not really make it smarter, just shorter by creating a list and take option as index in the list
First, you can look at this line:
q = int(input("Enter '1' if you want to continue or Enter '2' if you want to stop the test"))
this line happens on each of the options, so we don't need to repeat it. instead you can add it at the end of the "while" clause. also add "continue" to your "else" clause to avoid asking this when wrong input is entered.
also, the "else" clause for the "while" loop is not needed
e.g:
import speedtest
q = 1
while q == 1:
st = speedtest.Speedtest()
option = int(input("What do you want to test:\n 1)Download Speed\n 2)Upload Speed \n 3)Ping \n Please enter the number here: "))
if option == 1:
print(st.download())
elif option == 2:
print(st.upload())
elif option == 3:
servernames =[]
st.get_servers(servernames)
print(st.results.ping)
else:
print("Please enter the correct choice")
continue
q = int(input("Enter '1' if you want to continue or Enter '2' if you want to stop the test"))
print("Test is ended")
second, there is an error in your logic. your prompt says enter 1 to continue or 2 to quit, and indeed when you enter 2 the loop will end, but also when the user enters 3 or any other number. Even worse, if a user will enter a character that is not a number or nothing at all, they will get an exception. For this we use try-except clauses. another way to do this kind of loop is using "while "True" and then using "break" to exit.
while True:
... your code here ...
q = input("enter 1 or 2:")
try:
q = int(q)
except ValueError:
print("Invalid input")
if q == 2:
break
print("Test is ended")
This is helpful to you if:
you are a pentester
or you (for some other arbitrary reason) are looking for a way to have a very small python script
Disclaimer: I do not recommend this, just saying it may be helpful.
Steps
Replace all \n (newlines) by \\n
Replace two (or four) spaces (depending on your preferences) by \\t
Put """ (pythons long quotation marks) around it
Put that oneline string into the exec() function (not recommended, but do it if you want to)
Applied to this questions code
exec("""import speedtest\n\nq = 1\nwhile q == 1:\n\t\tst = speedtest.Speedtest()\n\t\toption = int(input("What do you want to test:\n 1)Download Speed\n 2)Upload Speed \n 3)Ping \n Please enter the number here: "))\n\t\tif\toption == 1:\n\t\t\t\tprint(st.download())\n\t\t\t\tq = int(input("Enter '1' if you want to continue or Enter '2' if you want to stop the test"))\n\n\t\telif option == 2:\n\t\t\t\tprint(st.upload())\n\t\t\t\tq = int(input("Enter '1' if you want to continue or Enter '2' if you want to stop the test"))\n\n\t\telif option == 3:\n\t\t\t\tservernames =[]\n\t\t\t\tst.get_servers(servernames)\n\t\t\t\tprint(st.results.ping)\n\t\t\t\tq = int(input("Enter '1' if you want to continue or Enter '2' if you want to stop the test"))\n\n\t\telse:\n\t\t\t\tprint("Please enter the correct choice")\nelse:\n\t\tprint("Test is ended")\n""")
I have found the solution to that by using the sys module. i suppose this could work if a wrong data is input.
import speedtest
import sys
#Loop for options
while True:
st = speedtest.Speedtest()
option = input("What do you want to test:\n 1)Download Speed\n 2)Upload Speed \n 3)Ping \n Please enter the number here: ")
#To check if the input value is an integer
try:
option = int(option)
except ValueError:
print("Invalid input")
continue
if option == 1:
print(st.download())
elif option == 2:
print(st.upload())
elif option == 3:
servernames =[]
st.get_servers(servernames)
print(st.results.ping)
else:
print("Please enter the correct choice: ")
continue
q = 0
#Choice to continue or to end
while (q != 1) or (q != 2):
q = input("Enter '1' if you want to continue or Enter '2' if you want to stop the test: ")
try:
q = int(q)
except ValueError:
print("Invalid input")
continue
if q == 1:
break
elif q == 2:
sys.exit("Test is ended")
else:
print("Invalid input")
continue
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.
I would like to check if my input is digit and in range(1,3) at the same time and repeat input till I got satisfying answer.
Right now I do this in this way, but the code is quite not clean and easy... Is there a better way to do this?
Maybe with while loop?
def get_main_menu_choice():
ask = True
while ask:
try:
number = int(input('Chose an option from menu: '))
while number not in range(1, 3):
number = int(input('Please pick a number from the list: '))
ask = False
except: # just catch the exceptions you know!
print('Enter a number from the list')
return number
Will be grateful for help.
I guess the most clean way of doing this would be to remove the double loops. But if you want both looping and error handling, you'll end up with somewhat convoluted code no matter what. I'd personally go for:
def get_main_menu_choice():
while True:
try:
number = int(input('Chose an option from menu: '))
if 0 < number < 3:
return number
except (ValueError, TypeError):
pass
def user_number():
# variable initial
number = 'Wrong'
range1 = range(0,10)
within_range = False
while number.isdigit() == False or within_range == False:
number = input("Please enter a number (0-10): ")
# Digit check
if number.isdigit() == False:
print("Sorry that is not a digit!")
# Range check
if number.isdigit() == True:
if int(number) in range1:
within_range = True
else:
within_range = False
return int(number)
print(user_number())
``
see if this works
def get_main_menu_choice():
while True:
try:
number = int(input("choose an option from the menu: "))
if number not in range(1,3):
number = int(input("please pick a number from list: "))
except IndexError:
number = int(input("Enter a number from the list"))
return number
If your integer number is between 1 and 2 (or it is in range(1,3)) it already means it is a digit!
while not (number in range(1, 3)):
which I would simplify to:
while number < 1 or number > 2:
or
while not 0 < number < 3:
A simplified version of your code has try-except around the int(input()) only:
def get_main_menu_choice():
number = 0
while number not in range(1, 3):
try:
number = int(input('Please pick a number from the list: '))
except: # just catch the exceptions you know!
continue # or print a message such as print("Bad choice. Try again...")
return number
If you need to do validation against non-numbers as well you'll have to add a few steps:
def get_main_menu_choice(choices):
while True:
try:
number = int(input('Chose an option from menu: '))
if number in choices:
return number
else:
raise ValueError
except (TypeError, ValueError):
print("Invalid choice. Valid choices: {}".format(str(choices)[1:-1]))
Then you can reuse it for any menu by passing a list of valid choices, e.g. get_main_menu_choice([1, 2]) or get_main_menu_choice(list(range(1, 3))).
I would write it like this:
def get_main_menu_choice(prompt=None, start=1, end=3):
"""Returns a menu option.
Args:
prompt (str): the prompt to display to the user
start (int): the first menu item
end (int): the last menu item
Returns:
int: the menu option selected
"""
prompt = prompt or 'Chose an option from menu: '
ask = True
while ask is True:
number = input(prompt)
ask = False if number.isdigit() and 1 <= int(number) <= 3 else True
return int(number)
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: ")