How to repeat an input until given suitable answer? [duplicate] - python

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 7 years ago.
I have finished a code, so that it asks the user to answer an arithmetic question and tell them if their answer is correct or not and so on.... I started doing some tests and realised if user enters anything then a number is gives me an error.
My Code:
import random
name=input("Welcome to this Arithmetic quiz,please enter your name:")
score = 0
for i in range(10):
number1=random.randint(20,50)
number2=random.randint(1,20)
oper=random.choice('+-*')
correct_answer = eval(str(number1)+oper+str(number2))
answer = (int(input('What is:'+str(number1)+oper+str(number2)+'=')) == correct_answer)
if answer:
print('Correct!')
score += 1
else:
print('Incorrect!')
print(name,"You got",score,"out of 10")
if score>1 and score<=3 :
print('Practice More!')
elif score>4 and score<=7 :
print('You did well!')
elif score>7 and score<=9 :
print('Excellent!')
elif score==10 :
print('You are a Genius!')
else:
print('Have you tried your best?')
I want to know how do I repeat line 9 until the user enters a number?
THIS IS NOT A DUPLICATE BECUASE I WANT THE USER TO SPECIFICALLY ENTER A NUMBER. IF HE/SHE DID IT WILL TELL THEM IT IS WRONG OR RIGHT AND MOVE ON TO THE NEXT QUESTION.

I would recommend putting the for loop in a while loop until true then exit the loop. Did that answer your question?

Related

Create a conversation that has a Y/N answer [duplicate]

This question already has answers here:
How do I do a case-insensitive string comparison?
(15 answers)
Closed 6 days ago.
I am trying to create a small bit of code in Python that runs a conversation, then asks a Y/N question i.e "Do you believe the sky is blue?"
When I run the code, it works well until I reach the question. It ignores my parameters for Y/N answers to print specific responses. It gives me the print response attached to my "else" statement.
I am confused if I am writing my If/elif/else statements wrong?
My code is written as follows:
x = input('do you believe the sky is blue')
if x == "yes":
print("I believe you are correct")
elif x == "no":
print('I think you have a unique perspective.')
else:
print('Please answer Yes or No.)
You use .lower() to obtain a lower case version of a string, and you are missing a ' in the last print():
x = input('do you believe the sky is blue ').lower()
if x == "yes":
print("I believe you are correct")
elif x == "no":
print('I think you have a unique perspective.')
else:
print('Please answer Yes or No.')

Input and if/else statements not processing correct input [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 1 year ago.
I'm putting together a small program for a friend that requires an input from the user, and depending on the input it does a certain function
Heres my code:
value = input ("Enter Number")
if value == 1:
print("You entered 1")
elif value == 2 :
print("You ented 2!")
else:
print("hmmm")
However, even entering 1 or 2, it always prints "hmmm".
I've tried everything including making a new function and passing the input into it and still it doesn't take. Any advice?
That's because you are taking input as a string not an integer.
Because of it your value is string and when it is compared with integer 1 or 2 it's coming false and the else condition gets satisfied.
Correct code:
value = int(input ("Enter Number"))
if value == 1:
print("You entered 1")
elif value == 2 :
print("You ented 2!")
else:
print("hmmm")

Problem in a number guessing program's while loop [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
I'm new to Yython programming. I create a simple program with random module, that ask for number, and person need to guess an number. I got problem with getting the answer. Even if I give the correct answer, program isn't stopping, here's the code:
import random
run = True
answer = random.randint(1,9)
guess = input("Give me an number in 1 to 9: ")
print(answer)
while run:
if guess == answer:
print("Congratulations, you won!\n" * 5)
run = False
else:
guess = input("Try again: ")
print(answer)
The print(answer) line is for me to know what is the answer, and even if I write it down, program isn't stopping.
answer is always an integer:
answer = random.randint(1,9)
and guess is always a string:
guess = input("Give me an number in 1 to 9: ")
thus they can never be equal.
You need to conver the inputted string to an integer:
guess = int(input("Give me an number in 1 to 9: "))
Or better yet, convert the generated random number to a string, to avoid the issue of the program crashing when the user inputs a non digit:
answer = str(random.randint(1,9))
The random function will return an integer and the input function will return a string in python, "1" is not equal to 1. To be able to check if the input is the same, convert the random number to a string by doing guess == str(answer) instead

The if statement goes automaticly to else statement [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
When I run the code and answer with 3, the console shows The answer is 3. This code is just an example, I worked on a code with random number.
I gave the input in the if statement, in a variable and i deleted else statement
answer = input("Answer of 1 + 2 = ")
if answer == 3:
print("You're right!")
else:
print("The answer was 3")
The right output would be You're right!
Typecasting to the rescue!
answer = input("Answer of 1 + 2 = ")
if int(answer) == 3:
# ^^^
print("You're right!")
else:
print("The answer was 3")
By default the value you get from input() is of type string. If you write 3 in your console, you get
answer = "3"
and
"3" != 3
You need to cast the input to int. Add this line before the if statement:
answer = int(answer)
Be careful to check that the typed value is actually an int (you can do that by using a try catch statement, or better with a while)

Exception handling on Python [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 6 years ago.
I'm new here, and I'm also new in coding.
I'm actually learning Python, and I have a question, because I already tried everything, but I was unable to resolve it.
I have this code from a little game I saw in a tutorial. The objective is to the user guess the number. What I was trying to do is to handle the exception if the user enters a letter, then show an error message and go back to the loop. If someone helps me, I will be grateful.
import random
highest = 200
answer = random.randrange(highest)
guess = raw_input("Guess a number from 0 to %d:" %highest)
while(int(guess)!=answer):
if (int(guess) < answer):
print "Answer if higher"
else:
print "Answer is lower"
guess=raw_input("Guess a number from 0 to %d: " %highest)
raw_input ("You're a winner Face!!!")
This is how i would do it:
import random
highest = 200
answer = random.randrange(highest)
while True:
try:
guess = int(input("Guess a number from 0 to %d: " %highest))
if guess < answer:
print("Answer if higher")
elif guess > answer:
print("Answer is lower")
else:
print("You're a winner Face!!!")
break
except:
print('Input not valid!')
continue
I have a dummy condition on the while and i am directing the flow from inside the loop using continue and break. I wrapped the whole guess checking procedure in a try-except block but the only thing that is really tried is the conversion of the input to integer. Everything else could also be moved after the except bit.

Categories

Resources