Python if statement gone wrong. Whats wrong? [duplicate] - python

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 1 year ago.
I don't understand what's wrong with my code here. Can someone help me pls? I've been trying to solve it all morning.
question = input("Choose from 0 to 1 : ")
mylist = ["Mark", "Jenny"]
if question == 0:
print(mylist[0], "is your new friend")
elif question == 1:
print(mylist[1], "is your new friend")
else:
print("I said choose from 0 to 1")

The problem is in the data types:
input() returns a string but in your, if statement you're comparing a string "0" to an integer 0. Because of that else is always executed.
Concert the input() into int() like shown below:
question = int(input("Choose from 0 to 1 : "))
mylist = ["Mark", "Jenny"]
if question == 0:
print(mylist[0], "is your new friend")
elif question == 1:
print(mylist[1], "is your new friend")
else:
print("I said choose from 0 to 1")

Related

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")

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)

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

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?

how come my math code always says that my answer is incorrect? [duplicate]

This question already has an answer here:
why does my math quiz always print incorrect when the answer is correct [closed]
(1 answer)
Closed 7 years ago.
i have just started programming and i am learning on how to make a math code that will tell the user if the answer is correct or not but the problem i am encountering is that even if i put the correct answer it prints my answer is incorrect and i have been trying to tackle the problem for several hours and cant find a solution.
this is my code in python:
print ("what is your username")
name = input () .title()
print (name, "welcome")
import random
score=0
question=0
for i in range(10):
ops = ["+", "-", "*"]
num1 = random.randint (0,10)
num2 = random.randint (0,10)
oparator = random.choice(ops)
Q=(str(num1)+(oparator)+(str(num2)))
print (Q)
guess = int(input())
if oparator =='+':
answer = (int(num1-num2))
elif oparator =='-':
answer = (int(num1-num2))
else:
oparator =='*'
answer = (int
(num1*num2))
if guess == Q:
print ("correct")
score + 1
else:
print ("incorrect")
im am really puzzled honestly, and help would be greatly appreciated
regards, kurt
You are comparing your guess (which is int) to the question Q (which is a str)
if guess == Q:
You want to compare
if guess == answer:
Also when you select '+' you have a mistake
if oparator =='+':
answer = (int(num1-num2))
You want to do addition
if oparator =='+':
answer = (int(num1+num2))
You also have indentation problems, among a variety of other issues.

Python - my else statement isn't working [duplicate]

This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Asking the user for input until they give a valid response
(22 answers)
Closed 8 years ago.
I am creating a text based game and asked for the gender. But it seems as if my else statement is not working.
def genderask ():
gender = input ("Are you a he or a she?")
try:
if gender == "he" or "she":
print("Hello, ", name)
else:
print("Your answer was not he or she.")
print
genderask ()
except ValueError:
print ("Your answer was not he or she.")
print
genderask
print
genderask ()
if gender == "he" or gender == "she":

Categories

Resources