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":
Related
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")
This question already has answers here:
Python: Problem with raw_input reading a number [duplicate]
(6 answers)
Python: not all arguments converted during string formatting
(3 answers)
Python TypeError: not all arguments converted during string formatting
(1 answer)
'%' python is giving me a "not all arguments converted during string formatting"
(1 answer)
Closed 4 years ago.
I searched about it and the other person who posted a similar issue had a typo error but I can't find that in mine.strong text
here's the code -
num = raw_input("Enter any number: ")
def is_even(x):
if x % 2 == 0:
return True
else:
return False
print is_even(num)
Convert your raw_input to int
Ex:
num = int(raw_input("Enter any number: "))
def is_even(x):
if x % 2 == 0:
return True
else:
return False
print is_even(num)
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)
How to test multiple variables for equality against a single value?
(31 answers)
Closed 5 years ago.
I'm asking input from the user and wanted to check if he used the right letters to continue, I've wrote this code, but even when I write "y" or "n", it doesn't exit the loop, and I have no idea why:
while True:
start = input("Want to start? (Y/n) ").lower()
if start != 'y' or 'n':
print("letter not accepted! Please write 'Y' to start or 'N' to exit!\n")
else:
break
if start == "y":
separate(players)
else:
print("Goodbye!")
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 6 years ago.
I'm constructing an interactive timetable for terminal with Python, but at the end of my code where i have if, elif and else statements no matter what user input i give it keeps passing the if statement. Any Solutions would be greatly appreciated and Thank You for your time :)
while True:
TimeTable()
print "\nDo you wish to go again? "
answer = raw_input()
if answer == "Yes" or "yes":
print " "
continue
elif answer == "No" or "no":
print "Ok then"
break
else:
print "Ok then"
break
answer == "Yes" or "yes"
# is the same as
(answer == "Yes") or "yes"
and is always True. You can solve your problem this way:
answer in ["Yes", "yes"]
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 6 years ago.
I'm not sure what I'm doing wrong. I want to make it so that if the user enters a name which isn't of those specified the program should print the else statement at the bottom. I also want the elif statements to work so if the users enter their age then the correct print statement should show.
name = input("What is your name?")
if name == "Jean Gray" or "Max Eisanhardt" or "James Howlett" or "Anne Marie":
age = int(input("How old are you?")
if age <= 12:
print ('You should be placed in the junior category because you are younger than 12 years old')
elif age >= 18:
print ('You should be placed in the senior category because you are older than 18 years old')
elif age <=17:
print ('You should be places in the teen category')
else:
print("Sorry, that name is not recognised.")
Your if statement will always currently evaluate to true, you should use the syntax
if a == "value1" or a == "value 2" or a == "value3":
//do something