This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 4 years ago.
I am trying to do triple nesting of while loops.
If you input a decimal number it returns error, then if you input number above 31 it returns error, but if you try again to input decimal number code stops. Need help making it indefinite loop no matter how many times, or what order, a user inputs incorrect format. Also need to verify that dates input are valid for number of days in given month?
import string
varD= input("Enter Date/Day:")
while varD.isdigit() or varD.isspace()\
or varD.isdecimal or int(varD)>31 \
or int(varD)==26 or int(varD)<=0:
print ("Error: Enter Valid Number!")
varD= input("Enter Day:")
else:
print ("You have entered:", varD)
Use an infinite loop and break only when all criteria are satisfied instead.
while True:
varD = input("Enter Day:")
if varD.isdigit() and not varD.isspace() and varD.isdecimal() \
and int(varD) < 32 and int(varD) != 26 and int(varD) > 0:
break
print("Error: Enter Valid Number!")
print("You have entered: %s" % varD)
Also, your understanding of the term triple nesting is incorrect. Triple nesting means something like this:
while expression1:
while expression2:
while expression3:
do_something()
Related
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 1 year ago.
This is what I tried and , basically what I was trying to do was that, if a user enters other than 1,2,3,4,6 then it will go back to a=int(input.....)). But for me it keeps giving output as None.
def hi():
while True:
try:
a=int(input("enter the denominator of Pi radian\n"
"(choose from 1,2,3,4,6)\n"
"Enter here:"))
if a <=0 or a>=7 and a!=5:
print("Enter the given digits")
else:
return a
except Exception:
print("enter a valid type")
ant=hi()
print(ant)
You have applied a wrong condition here:
if a <=0 or a>=7 and a!=5:
You're checking a!=5 as a wrong input, when it should be a==5. Also apply or instead of and. Change it to:
if a <=0 or a>=7 or a==5:
This question already has answers here:
Fastest way to check if a value exists in a list
(11 answers)
Closed 2 years ago.
Hi I am new to programming and I like to try to make the code work different way.
Unfortunately, variable called unlucky cant be read when printing.
So when the user enter the listed value [7 6 5], it doesnt print out as "You have selected unlucky number". I have include the image and copy of my code :
option = int(input("Guess the correct number between 0 to 10 : \nYour choice :"))
unlucky_number= [7,6,5] # Unlucky numbers, listed
if option == unlucky_number: # should print somewhere close when user enters list number
print("You have selected a unlucky number")
elif option== 6: # only 6 is correct
print ("Correct Guess")
elif option in range (0,4):
print("Not close")
else:
print ("Not in the range, choose between 1 to 10")
Please tell me whats wrong with it and how can I make a better version of it.
Thank you enter image description here
if option == unlucky_number
This line is causing you troubles. Your "option" holds a singular number, but your "unlucky_number" is a list. List can not be equal to a number, they are completely different animals. What you want is:
if option in unlucky_number
This question already has answers here:
Specify input() type in Python?
(2 answers)
Closed 4 years ago.
I am completely new to programming. However, I just wanna write a simple bit of code on Python, that allows me to input any data and the type of the data is relayed or 'printed' back at me.
The current script I have is:
x = input()
print(x)
print(type(x))
However, regardless of i input a string, integer or float it will always print string? Any suggestions?
In Python input always returns a string.
If you want to consider it as an int you have to convert it.
num = int(input('Choose a number: '))
print(num, type(num))
If you aren't sure of the type you can do:
num = input('Choose a number: ')
try:
num = int(num)
except:
pass
print(num, type(num))
1111 or 1.10 are valid strings
If the user presses the "1" key four times and then Enter, there's no magic way to tell if they wanted to enter the number 1111 or the string "1111". The input function gives your program the arbitrary textual data entered by user as a string, and it's up to you to interpret it however you wish.
If you want different treatment for data in particular format (e.g. if they enter "1111" do something with it as a number 1111, and if they enter "111x" show a message "please enter a valid number") then your program needs to implement that logic.
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 5 years ago.
I am using Python 3.6 currently and my code currently is asking a user to input 2 integers and convert them into binary form, this is not a problem, the issue that I have is when it asks the user to input a number, if they put anything other than an integer, it will break the code and come up with an error, is there a specific few lines of code that will ensure that errors will not display if letters or floats are inputted?
def add():
Num1 = int(input("Input the first number between 0 and 255, the calculated answer must not be above 255: "))
Num2 = int(input("Input the second number between 0 and 255, the calculated answer must not be above 255: "))
I have tried one method which was:
if type(Num1) != int:
print("Please input a number")
add()
However I still get errors when testing the code.
I would like the code to detect that a string or float has been inputted, then reload the "def" function without giving a syntax error.
This is the print result:
This is the gist of how to get an integer from the command line. It works using a while loop and try/except statements. You can add the checks for the integer range. You can use this code to create a function, which can be called twice for your particular problem.
n = None
while True:
n = input('enter an integer')
try:
n = int(n)
break
except:
continue
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 7 years ago.
Ptarget = int(input("What is your target amount of points to achieve?"))
while Ptarget != int:
print("You have not provided a valid input, please try again.")
Ptarget = int(input("What is your target amount of points to achieve?"))
How do I make it so the while loop functions, by asking the user for another input if the previous was not an integer, without breaking the program?
Use an exception
while True:
try:
Ptarget = int(input("What is your target amount of points to achieve?"))
break
except:
print("You have not provided a valid input, please try again.")