This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 3 years ago.
Just i did somethin wrong with while loop i supposed
After print menu section u can input something but than program ends.
print("Welcome in American Roulette")
rules=("blabla rules of roulette")
import random
while True:
print("Enter 'rules' to show rules")
print("Enter 'play' to play the game")
print("Enter 'author' to show info about creator")
print("Enter 'quit' to end the program")
user_input=input()
if user_input=="quit" or "Quit":
break
elif user_input=="rules" or "rule" or "Rules" or "Rule":
print(rules)
elif user_input=="play":
bet=int(input("Place your bet\n"))
amount=float(input("How much you want to bet?"))
spin=random.randint(0,36)
if bet>36:
print("You need to bet number beetwen 0 and 36!")
elif bet<0:
print("You need to bet number beetwen 0 and 36")
elif spin==bet:
print("You won",amount*35)
print("Now you have",start-amount+amount*35)
else:
print("You lose")
elif user_input=="author":
print("Jacob X")
else:
print("Unknown command")
Added some text here becouse my code is mostly code ffs
Your first if should be:
if user_input.lower() == "quit":
break
or
if user_input == "quit" or user_input == "Quit":
break
You are missing the second user_input on the conditional. As "Quit" is not an empty string it evaluates to True and your while loop will break.
This is also the case for this elif:
elif user_input=="rules" or "rule" or "Rules" or "Rule":
Related
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 1 year ago.
enter image description here
i want to use function in simple prigram
but it`s giving me wrong result in a wrong way
i wanna make a loop with function and ask as many time i want to see if the number i enterd is odd or even
def new_game():
game()
def game():
a = int(input("please enter the number... "))
if a % 2 == 0:
print(a, " Is even ")
else:
print(a, " Is odd ")
game()
def check():
ask = input("Do you want to continue ? yes/no ").lower()
if ask == 'yes':
return True
elif ask == 'no':
exit()
else:
print('Bye')
while check():
game()
check()
break
You actually had it. But the break in your while loop is not needed and is what is causing your problem. Say you entered y, it will exit check() and break. Remove that.
while check():
game()
check()
#break is gone
full code
def game():
a = int(input("please enter the number... "))
if a % 2 == 0:
print(a, " Is even ")
else:
print(a, " Is odd ")
def check():
ask = input("Do you want to continue ? yes/no ").lower()
if ask == 'yes':
return True
elif ask == 'no':
exit()
else:
print('Bye')
game()
while check():
game()
check()
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 2 years ago.
#I want the While function to show only when the player has not written 'run' or 'Attack'. What can I do to correct this?
action = input("A goblin ambushes you, what do you do? ")
if action == "Run":
print("You run and get away!")
if action == "Attack":
print("You hit the Goblin, knocking it out!")
while action != "Attack" or "Run":
action = input("That is not an action, what do you do? ")
If I understand correctly, this should do the trick:
action = input("A goblin ambushes you, what do you do? ")
while action != "Attack" and action !="Run":
action = input("That is not an action, what do you do? ")
if action == "Run":
print("You run and get away!")
elif action == "Attack":
print("You hit the Goblin, knocking it out!")
Is there a way to allow only integers except one specific string? I wrote a number guessing game in python and if the player decides to quit the game, the game should break through the user input "quit", while allowing only numbers during the guessing process.
Thank you in advance!
Check if input is digit and not 'quit' then continue game
print("Insert your number")
user_input = input()
if user_input.isdigit():
#continue your game
elif user_input == 'quit':
#quit game
else:
print("Invalid option. Type in a number or 'quit' for exit")
how about just checking for either numeric or 'quit'?
if input.isnumeric():
#proceed as with number input
elif input == 'quit':
#quit
else:
#invalid input
This question already has answers here:
How can I break out of multiple loops?
(39 answers)
How can I fix the if statements in my while loops?
(1 answer)
Closed 5 years ago.
I am wondering if someone can help me figure out how to fully break out of my while loop(s) and continue with the rest of my program. Thanks!
import time
while True:
company_name = input("\nWhat is the name of your company? ")
if company_name == "":
time.sleep(1)
print("\nThis is not eligible. Please try again")
else:
while True:
verify_name = input("\nPlease verify that {} is the correct name of your company \nusing Yes or No: ".format(company_name))
if verify_name.lower() == "no":
print("\nPlease re-enter your company name.")
time.sleep(1)
break
elif verify_name.lower() not in ('yes', 'y'):
print("\nThis is an invalid response, please try again.")
time.sleep(1)
break
else:
print("\nWelcome {}.".format(company_name))
verify_name == True
break
else:
break
#Continue with rest of my program
The solution below adds a flag to control when to break out of the external loop that is set to break out each loop, and set back if no break has occurred in the internal loop, i.e. the else statement has been reached on the inner loop.
import time
no_break_flag = True
while no_break_flag:
no_break_flag = False
company_name = input("\nWhat is the name of your company? ")
if company_name == "":
time.sleep(1)
print("\nThis is not eligible. Please try again")
else:
while True:
verify_name = input("\nPlease verify that {} is the correct name of your company \nusing Yes or No: ".format(company_name))
if verify_name.lower() == "no":
print("\nPlease re-enter your company name.")
time.sleep(1)
break
elif verify_name.lower() not in ('yes', 'y'):
print("\nThis is an invalid response, please try again.")
time.sleep(1)
break
else:
print("\nWelcome {}.".format(company_name))
verify_name == True
break
else:
no_break_flag = True
#Continue with rest of my program
Obviously as you have a condition of while True on the inner loop you will always exit by breaking, but if you had some other condition this would break the external loop only if a break statement was reached on the inner loop.
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 5 years ago.
Hello i made this program that asks the user to input the choices Print,sort1,sort2 or quit
usrinpt=0
while usrinpt != "quit": #if usrinpt == quit then no if statements right?
usrinpt = input("Enter choice Print, Sort1 (distance), Sort2 (price) or Quit : ")
usrinpt = usrinpt.lower()
if usrinpt == "print":
PrintList(mat)
pass
elif usrinpt == "sort1":
SelectionSortDistorPrice(mat,0)
PrintList(mat)
pass
elif usrinpt == "sort2":
SelectionSortDistorPrice(mat, 1)
PrintList(mat)
pass
elif usrinpt != "quit"or"sort1"or"sort2": #why does it print the string even when i enter quit?
print("please enter a valid choice")
pass
expected outcome for the choice "quit" is for a the program to stop
actual outcome is that the program prints " please enter a valid choice" and then quits
how can i fix it to only print that string if a choice not stated is entered?
elif usrinpt not in ["quit","sort1","sort2"]: