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.')
Related
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 17 days ago.
I have written this very simple code. I am new to programming. But I seem to dont understand why this loops doesnt break when I give it "N" for an answer when I run it.
while (True):
name = input("What is your name?\n")
print(f"Hello {name}, how are you today?")
answer = input("would you like to continue with the conversation? Reply with 'Y' or 'N'\n")
if answer == "y" or "Y":
continue
elif answer == "N" or "n":
break
else:
print("Kindly only answer with 'Y' or 'N'")
I wanted this to get out of loop and break the program when I enter "N"
You need to include answer == in both sides of the conditionals.
i.e.
while (True):
name = input("What is your name?\n")
print(f"Hello {name}, how are you today?")
answer = input("would you like to continue with the conversation? Reply with 'Y' or 'N'\n")
if answer == "y" or answer == "Y":
continue
elif answer == "N" or answer == "n":
break
else:
print("Kindly only answer with 'Y' or 'N'")
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)
Closed 6 months ago.
The following code snippet is meant to allow the user to input the answer to a question. They are allowed to enter four
answers: either y or Y for “yes”, or n or N for “no”. The program is supposed to print out the received answer if the
entry is valid, and print out an error message otherwise.
answer = input("What is your answer? ")
if answer == "y" or "Y":
print("You answered yes")
elif answer == "n" or "N":
print("You answered no")
else:
print("You didn’t enter an acceptable answer")
It just keeps on saying that I answered yes regardless if I put n or N or some random thing. Can someone explain it to me?
The precedence of the or is not what you are expecting. Instead try:
answer = input("What is your answer? ")
if answer in ("y", "Y"):
print("You answered yes")
elif answer in ("n", "N"):
print("You answered no")
else:
print("You didn’t enter an acceptable answer")
Or maybe like:
answer = input("What is your answer? ")
if answer.lower() == "y":
print("You answered yes")
elif answer.lower() == "n":
print("You answered no")
else:
print("You didn’t enter an acceptable answer")
Your first condition will always return true because "Y" is always truthy.
Try: if answer == "y" or answer == "Y":
And the same modification for the other conditional.
If you want to check a variable shortly as you want, use something like this:
if answer in ["y","Y"]:
It will return True if answer is y or Y
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 6 years ago.
For some reason my program keeps rolling the dice again regardless of the users answer, and it doesn't print the goodbye message.
I'll include the code below so you can test yourself (Python 3)
from random import randint
def rollthedice():
print("The number you rolled was: "+ str(randint(1,6)))
rollthedice()
shouldwecontinue = input("Do you wish to roll again? (y or n) ").lower()
if shouldwecontinue == "y" or "yes":
rollthedice()
elif shouldwecontinue == "n" or "no":
print("Goodbye.")
elif shouldwecontinue != type(str):
print("Sorry the program only accepts 'y' or 'n' as a response.")
The desired effect of the program should be that if the user enters y, it rolls the dice again, respectively starting the whole program again. Whereas, if the user enters no then it should print the Goodbye message.
or doesn't work like that. Right now your program is evaluating (shouldwecontinue == "y") or "yes", and since Python interprets "yes" as truthy, then that condition always succeeds. The simplest fix is to change it to shouldwecontinue == "y" or shouldwecontinue == "yes". And then similar changes for the other conditional expressions, of course.
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:
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?