What is wrong with my while loop [duplicate] - python

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 6 years ago.
This is a code i am working on but when i execute it , creates a problem towards the end
restart = "No" or "no"
while restart == "No" or "no":
print("League Registration")
Fristname = input("What is first name?")
Lastname = input("What is your Last name?")
Nickname = input("What is your nick name?")
Eaddress = input("What is your e-mail address?")
Skill = input("What is your skill level, E for expert or C for casual?")
if Skill == "C" or Skill == "c":
print("Casual")
elif Skill == "E" or Skill == "e":
print("Expert")
print (" These are your personal details:")
print ("First Name:",Fristname)
print("Last Name:",Lastname)
print("Nickname:",Nickname)
print("Email Address:",Eaddress)
print("Skill Level:",Skill)
Detailscon = input("Are your personal details correct: Yes or No?")
if restart == "Yes" or "yes":
print("Thanl you , you are now registered")
elif restart == "No" or "no":
print("Try again")
my code towards the end keeps messing up i don't know what to do

The error in your code, lies here. You see, in python
while (restart == "No") or "no":
The operator equal will evaluate first and then check if it is true or if the string "no" is longer than zero, which it is so it will run forever.
Instead change it to.
while restart in ["No", "no"]:

Related

IndentationError: expected an indented block after function definition on line 12 [duplicate]

This question already has answers here:
I'm getting an IndentationError. How do I fix it?
(6 answers)
Closed 8 months ago.
I think that my def main() and my answer1 are having a conflict because in my code my first "answer1" makes an error and I'm not able to execute the command can someone tell me what is the problem? And what I can do?
print("Welcome to my computer game!")
playing = input("Would you like to play? ")
if playing != "yes":
quit()
print("Nice! Let's play! ")
print("Answer true or false to these phrases.")
def main():
answer1 = input("Hugo Antunes is busy! ")
if answer1 == "false":
print("Wrong, you should know your Hugo better!")
if answer1 == "true":
print("Correct! ")
if any((answer1 != "true", answer1 != "false")):
main()
answer2 = input("Next phrase. Hugo's favorite color is yellow. ")
Notice that main is a function so, you have to use indentation to make the function's scope.
Do you want something like this?
def main():
answer1 = input("Hugo Antunes is busy! ")
if answer1 == "false":
print("Wrong, you should know your Hugo better!")
if answer1 == "true":
print("Correct! ")
if any((answer1 != "true", answer1 != "false")):
main()
...

Why is python is running the if statement when it should be running elif [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)
Closed last year.
I'm still learning the basics of python and can't seem to figure out why my simple dice rolling code isn't working properly.
import random
def main():
num = int(input("How many sides of the die?\n"))
num_of_rolls = int(input("How many rolls?\n"))
roll_y_n = input("Are you ready to roll? Y/N\n")
var = 1
def dice_roll(num):
return random.randrange(1,num)
if roll_y_n.lower() in "yes" or "y":
while var <= num_of_rolls:
print("You rolled a " + str(dice_roll(num)) + ".")
var = var + 1
else:
print("Okay, Byeee!")
exit()
retry = input("Do you want to go again? Y/N\n")
if retry.lower() == "yes" or "y":
main()
elif retry in "no" or 'n':
print("Okay, Byeee!")
exit()
main()
it runs it just fine, but when ever I type in "n" to get it to stop it still runs as if I typed in "y".
Sorry if this is a dumb question, I just can't figure it out.
There is lot of syntax error in your code:
Use this:
if roll_y_n.lower() in ["yes", "y"]:
while var <= num_of_rolls:
print("You rolled a " + str(dice_roll(num)) + ".")
var = var + 1
if retry.lower() in ["yes","y"]:
main()
elif retry in ["no", 'n']:
print("Okay, Byeee!")
exit()

Ending a "While" loop python? [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)
Closed 2 years ago.
This might be a really dumb question, but I can't see why this loop doesn't end. I'm aware that while True is an infinite loop, but I'm pretty sure there are also ways to get out of them. Does anyone know how to end this code, so that if they enter "yes" etc. it continues on through the program but if they enter "no" etc. or anything else it asks them for their name again
while True:
user_name = input("What do you want your name to be? (Suggest using Forename and Surname!")
user_name_check = input("Your name is " + user_name + "? Is this correct?")
if user_name_check == "yes" or "Yes" or "y" or "Y":
break
elif user_name_check == "no" or "No" or "n" or "N":
pass
else:
print("Sorry, please only enter yes or no. Re-enter your name and try again.")
pass
try
if user_name_check == "yes" or user_name_check == "Yes" or user_name_check == "y" or user_name_check == "Y":

why multiple condition in python while loop are not working? [duplicate]

This question already has answers here:
OR statement handling two != clauses Python
(5 answers)
Closed 3 years ago.
I can't seem to find the problem in code...
user_decision = ""
while not user_decision == "yes" or not user_decision == "no":
user_decision = input("You want to Join?: Please answer (yes/no): ")
else:
if user_decision == "yes":
print("test")
else:
print("test")
Thanks....
I cleaned it up a bit:
user_decision = ""
while (user_decision != "yes") and (user_decision != "no"):
user_decision = input("You want to Join?: Please answer (yes/no): ")
if user_decision == "yes":
print("test yes")
else:
print("test not yes")

Python 'if' statement not triggering correctly [duplicate]

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, no matter what user_input is (Yes, No, "", eserw3) the first if statement will always be triggered. Any insight as to why the elif and the else never get activated? (The below code compiles perfectly without any errors)
Thank you in advance.
def retry():
user_input = raw_input("Would you like to face %s again? (Yes/No)" % (Enemy))
if user_input == "Yes" or "yes":
respawn()
getMove()
elif user_input == "No" or "no":
print "Thanks for playing!"
else:
print "Please enter either Yes or No."
def retry():
user_input = raw_input("Would you like to face %s again? (Yes/No)" % (Enemy))
if user_input == "Yes" or user_input == "yes":
respawn()
getMove()
elif user_input == "No" or user_input == "no":
print "Thanks for playing!"
else:
print "Please enter either Yes or No."
def retry():
user_input = raw_input("Would you like to face %s again? (Yes/No)" % (Enemy)).lower()
if user_input == "yes":
respawn()
getMove()
elif user_input == "no":
print "Thanks for playing!"
else:
print "Please enter either Yes or No."
Change your if condition to
user_input in ["Yes", "yes"]
Reason: When you write user_input == "Yes" or "yes", it evaluates as:
(user_input == "Yes") or "yes"
The second part of OR is a True always(non-zero length string). Hence your problem of if block executing always.

Categories