This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 8 years ago.
I am attempting to create a basic program which requests a numeric value from the user.
If the value is between .5 and 1 the program should print "good".
If the value is between 0 to 0.49 the output states "fair".
If the numeric input the user provides is outside of 0 to 1 it states: "try again".
If the input cannot be converted to a number it states: "Invalid input".
Here is what I have got so far:
val=abs(1)
while True:
num = raw_input("Enter a number: ")
if num == "val" : break
print 'try again between 0 to 1'
try:
num = float(num)
except:
print "Invalid input"
if .5 < num < 1:
print 'Good'
if 0 < num < .49:
print 'Fair'
There's a couple issues with your code. I've cleaned up your code with regards to what I think what you actually want to do and commented most of the changes. It should be easily adjustable if you have slightly different needs.
val = 1 # abs(1) is the same as 1
while True: # get user input
num = raw_input("Enter a number: ")
try: # indented
num = float(num) # goes to except clause if convertion fails
if not 0 <= num <= val: # check against val, not "val",moved to try block
print 'try again between 0 to 1' # indented
else:
break # input ok, get out of while loop
except ValueError: # indented, only excepting ValueErrors, not processor is burning errors
print "Invalid input"
if .5 <= num <= 1:
print 'Good'
else: # 0 <= num < 0.5
print 'Fair'
Related
gone = []
turn = 0
def play(XO,player):
while 0 == 0:
num = input("\n"+player+" enter an available number where you want to put an '"+XO+"'.\n > ")
while ((str(num).isdigit() == False) or ((int(num) <= 9 and int(num) >= 1) == False)) or (num in gone):
num = input("\nValue was not an available number.\n"+player+" enter an available number where you want to put an '"+XO+"'.\n > ")
So, in the second while loop I'm having a problem. You see the (num in gone) part? I'm trying to make it so if num is found in the gone list then it will be true, but it isn't working. Instead it passes through it.
I've tested to see if (not num in gone) applies the opposite effect, and it does!
If you need to see my entire code I can post it... btw this is for a Tic-Tac-Toe program I am making.
You're putting too much logic in one condition. Splitting it up will help you a lot.
Try something like this:
gone = []
turn = 0
def play(XO, player):
while True:
num = input(
f"\n{player} enter an available number "
f"where you want to put an '{XO}'.\n > "
)
try:
num = int(num)
except ValueError:
print("Not a valid number, try again")
continue
if num < 1 or num > 9:
print("Number not in correct range, try again")
continue
if num in gone:
print("Number already gone, try again")
continue
gone.append(num)
turn += 1
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 3 years ago.
If value is 1-100 it stops program
If value <1 or >100 i need to print (try again) and loop back to top.
This what i got atm
n1 = 0
n1 = int(input("Enter number between 1-100: "));
print ("your number is: ", n1);
while n1 > 100 or n1 < 0:
print("Try again");
This is now how python works
First of all no need to initiate n1 and then assign input value to it
You should have used if instead of while
while True :
try :
n1 = int(input ())
except :
print ("enter numerical value")
if n1<1 or n1>100 :
print ("try again")
else :
break
Mind indentation
Try this,
while True:
n1 = 0
n1 = int(input("Enter number between 1-100: "));
if n1 < 100 and n1 > 0:
break
else:
continue
and please don't use ; end of your code.
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 5 years ago.
I'm trying to write a code where the program keeps getting the user input in integers and adds it to an empty array. As soon as the user enters "42", the loop stops and prints all the collected values so far.
Ex:
Input:
1
2
3
78
96
42
Output:
1 2 3 78 96
Here is my code (which isn't working as expected):
num = []
while True:
in_num = input("Enter number")
if in_num == 42:
for i in num:
print (i)
break
else:
num.append(in_num)
Here is one solution, which also catches errors when integers are not entered as inputs:
num = []
while True:
try:
in_num = int(input("Enter number"))
if in_num == 42:
for i in num:
print(i)
break
except ValueError:
print('Enter a valid number!')
continue
else:
num.append(in_num)
I think your issue is with the if condition: Input builtin function returns a string and you are comparing it with an integer.
in_num = None
num = []
while True:
in_num = input("Enter number: ")
if in_num == "42": #or you could use int(in_num)
for i in num:
print(i)
break
else:
num.append(in_num)
The problem is, that input is giving you a string and you check in line 5 for an int.
You have to replace it to if in_num == "42" or directly convert your input("Enter number") to an int using int(input("Enter number"))
This question already has answers here:
How can I convert a string to an int in Python?
(8 answers)
Closed 8 years ago.
while True:
a = raw_input("Your number: ")
if a == int: # letters:
if a > 0 and a % 3 == 0:
print 'fizz'
if a > 0 and a % 5 == 0:
print 'buzz'
if a > 0 and a % 3 != 0 and a % 5 != 0:
print a
if a <= 0:
print 'bad value'
else:
print "Not integer, try again"`
How do I make this raw_input work? I want this to run the game when the user input is an integer and "try again" when it is not.
raw_input() always returns a string. If you want to make it an int, call the int() builtin function. If the contents of the string cannot be converted, a ValueError will be raised. You can build the logic of your program around that, if you wish.
raw_input is a string. You can convert it to an int using int. If it's not convertible, this returns an error. So use try... except to handle the error. It's a good idea to put as little as possible into the try... part because otherwise some other error might be accidentally caught. Then put in a continue into the except part to skip back to the start.
while True:
try:
a= int(raw_input("Your number: "))
except ValueError:
print "not integer, try again"
continue
if a > 0:
if a % 3 == 0:
print 'fizz'
if a % 5 == 0:
print 'buzz'
if a % 3 != 0 and a % 5 != 0:
print a
else: #a<=0
print 'bad value'
I'm kind of new to coding in Python and I'm trying to write a loop that asks for numerical input and reads it until the user inputs "done", where it will then exit the loop and print the variables: number, count and average. (not trying to store anything in a list)
I also want it to print "invalid input" and continue the loop if the user enters anything that isn't an integer, unless its "done".
Unfortunately it returns "invalid input" and keeps looping even when I enter "done". What am I doing wrong? Could anyone point me in the right direction?
number = 0
count = 0
avg = 0
inp = 0
while True:
try:
inp = int(raw_input('Enter a number: '))
if inp is 'done':
number = number + inp
count = count + 1
avg = float(number/count)
break
except:
print 'Invalid input'
print number
print count
print float(avg)
sum = 0.0
count = 0
while True:
inp = raw_input('Enter a number: ')
if inp == 'done':
break
try:
sum += int(inp)
count += 1
except ValueError:
print 'Invalid input'
print sum
print count
if count != 0: # Avoid division by zero
print sum / count