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)
Related
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.')
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")
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
I am creating a math test program that creates random questions and I ask the user to input the answer and the answer is checked with the correct answer and the output is given.
import random
q = 1
live=3
operations = ("+" , "-")
for q in range(0,11):
while live>=1:
op=random.choice(operations)
num1=random.randint(0,10)
num2=random.randint(1,10)
if op =="+":
op=random.choice(operations)
num1=random.randint(0,10)
num2=random.randint(1,10)
correct = num1+num2
print("What is", " " +str(num1) + "+" +str(num2) + "?")
answer =input("\n Enter answer")
if answer==correct :
print("Correct answer")
q= q+1
else:
answer != correct
live= live - 1
print("The answer is incorrect. Try again mate!")
this is my incomplete code. The problem is even when there is a question with the addition mark and the user inputs the correct answer the programs still prints the wrong output (the output that should be printed if the user enters a wrong answer) Please, any help would be appreciated. Thanks in advance.
Simply change this line:
answer = input("\n Enter answer")
To this:
answer = int(input("\n Enter answer"))
The reason being is that your comparing a str and an int.
For Example
In the case ofnum1 = 5 and num2 = 5
correct will be equal to 5+5 or 10.
However, answer will be equal to the string "10".
So when you go to test equality between correct and answer, you're comparing "10" == 10 which is of course False. Thus you need to typecast the string answer into an int because input always returns a string. This results in 10==10 which is True.
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?