This is a simple one:
while True:
a = input("Enter yes/no to continue")
if a=="yes":
gameplay()
continue
elif a=="no":
break
else:
print("Enter either yes/no")
Where gameplay function contains the code to be executed
Answer from Aswin Murugesh on Stack OverflowThis is a simple one:
while True:
a = input("Enter yes/no to continue")
if a=="yes":
gameplay()
continue
elif a=="no":
break
else:
print("Enter either yes/no")
Where gameplay function contains the code to be executed
I would do it the following way:
while True:
# your code
cont = raw_input("Another one? yes/no > ")
while cont.lower() not in ("yes","no"):
cont = raw_input("Another one? yes/no > ")
if cont == "no":
break
If you use Python3 change raw_input to input.
(Solved) I'm trying to ask the user if they want to continue by typing in either Y/y for yes or N/n for no. If i type Y twice it sends me to my print(Thank you for playing). What did I do wrong here?
def dmg ():
import random
damage = random.randint(1,12)
longSword(damage)
def longSword(damage):
cont = input("Would you like to attack? (Type Y or y for yes and N or n for N)")
while cont == "y" or "Y":
print('Your longsword hit for', damage, "damage")
cont = input("Would you like to attack again?")
if cont == "n" or "N":
print('Thank you for playing')
break
dmg ()
Newbie question - Programming help
Using a while loop to ask the user (Y/N) ?
while loop help...
(I'm going to try not to give you the answer, but to push you towards it)
You want the while loop to run over and over again until the user inputs neither yes or no, right? So you'd want to break out of the loop if the answer is yes, break out of the loop if the answer is no, else try again, so you need to set up the condition for exit, for entry, and the logic in between.
As for where you'd put the while loop, you want to put it on the global line, then all your logic indented inside.
More on reddit.compython - Simple Yes or No Loop Python3 - Stack Overflow
Videos
So I have a program that calculates compound interest, and now I need to add a question at the end of my program that asks the user if they would like to run the program again, if they answer with Y or y the program will restart to the beginning, if they answer “N” or “n” the program is over. And if they answer with anything other than y or n it should repeat the question until the user finally enters y or n
What I have so far:
I have my entire program in a while loop and was able to get it to reset while Var == Y or y. But I can’t get it to repeat the question if the user answers with let’s say “q” or “618” or “L”. Does anyone know how to do something like this, please let me know… thank you
Hello anyone, I'm trying to write a program that asks the user if he/she wants to play a game. The user has the option of only entering 'yes' or 'no' and if it's anything other than that, the program should enter a while loop that asks the user to "please enter a valid answer of either 'yes' or 'no'. So far I have this:
print("would you like to play a game?")
answer = raw_input()
if answer == "yes":
print("Let's play!!")
elif answer == "no":
print("Ok, another day...")
else:
answer = False
But where to put the while loop??
Update: Ok guys, thanks for your help. I got!
print("would you like to play a game?")
answer = None
while answer != 'yes' or answer != 'no':
answer = raw_input("Please enter yes or no: ")
if answer == "yes":
print("Let's play!!")
else:
print("Ok, another day...")
I was also trying write a program that has a definition. So far I have this
def game(answer):
if answer == 'yes':
print("Ok, let's begin!")
elif answer == 'no':
print("Ok, another time then...")
print("Would you like to play a game?")
answer = input()
while True:
if answer != 'yes' or answer != no:
answer = input("Please enter a valid response ")
continue
else:
break
game(answer)
but I don't know what I'm doing wrong in block 5.
(I'm going to try not to give you the answer, but to push you towards it)
You want the while loop to run over and over again until the user inputs neither yes or no, right? So you'd want to break out of the loop if the answer is yes, break out of the loop if the answer is no, else try again, so you need to set up the condition for exit, for entry, and the logic in between.
As for where you'd put the while loop, you want to put it on the global line, then all your logic indented inside.
Remember you can use 'or' when evaluating.
You are first calling yes_or_no, which outputs a value but you throw it away and are calling the function again instead of testing on the output of the first call.
Try storing the output in a variable.
# Store the output in a variable
answer = yes_or_no()
# Conditional on the stored value
if answer == 1:
print("You said yeah!")
else:
print("You said nah!")
Side notes
It is considered bad practice to use capitalized names for variables, those should be reserved for classes.
Also, a user-prompt loop is better implemented with a while-loop to avoid adding a frame to your call-stack every time the user enters a wrong input. This is unless you use an interpreter which implements tail-call optimization, which cPython does not.
Here is what an improved version of your function could look like.
def yes_or_no():
while True:
answer = input("Yes or No?").lower()
if answer == "yes":
return 1
elif answer == "no":
return 0
You can use break and continue statements like this:
def yes_or_no():
YesNo = input("Yes or No?")
YesNo = YesNo.lower()
if(YesNo == "yes"):
return 1
elif(YesNo == "no"):
return 0
else:
return -1
while(True):
inp = yes_or_no()
if(inp == -1):
continue
elif(inp == 1):
print("You said yeah!")
elif(inp == 0):
print("You said nah!")
break
Try:
def yes_or_no(question):
reply = str(input(question+' (y/n): ')).lower().strip()
if reply[0] == 'y':
return 1
elif reply[0] == 'n':
return 0
else:
return yes_or_no("Please Enter (y/n) ")
print("started")
while True:
# DRAW PLOT HERE;
print("See plot....")
if(yes_or_no('Do you like the plot')):
break
print("done")
Best to keep function definition separate from loop for clarity. Also, otherwise it will be read in every loop wasting resources.
Output:
$ python ynquestion.py
started
See plot....
Do you like the plot (y/n): n
See plot....
Do you like the plot (y/n): N
See plot....
Do you like the plot (y/n): NO
See plot....
Do you like the plot (y/n): No
See plot....
Do you like the plot (y/n): no
See plot....
Do you like the plot (y/n): yes
done
$
You should definitely look at some tutorials about "how to code" in general. There are several "misconceptions". However, here is a cleaner version:
import matplotlib.pyplot as plt
import numpy
def bunch_of_math():
...
def plotting():
...
# move this into the loop in case you want to calc and plot
# new stuff every iteration
bunch_of_math()
plotting()
print("start")
while True:
reply = str(input(question+' (y/n): ')).lower().strip()
if reply == 'y':
break
elif reply == 'n':
break
else:
print("please select (y/n) only")
continue
print("done")
It is bad style to declare a function inside a loop, especially if you do not need this. Your code would re-create the function at each iteration, which you would only need if you somehow alter your function in each iteration.
reply[0] = 'n' means that you want to access the list or array (a container data structure) reply with the index 0 and write 'n' there. You have not initialized such a container. Additionally, you do not need a container at all, because you do not store each user input. You just care for the most recent answer of you user -> a variable is enough.
if reply[0] == 'y':
return 1
if reply[0] == 'n':
return 0
else:
return yes_or_no("Please Enter (y/n) ")
You have two if conditions after another: Python would check for == 'y' and then always check again for == 'n'. You need to use elif to declare an else-if condition, otherwise you waste resources or ran into unexpected behavior. Additionally, you are never using the return values. The while-loop just exits with a break statement, because it is a loop. Thus, your return statements are pointless.
//This code is for explanation
quiz = str(input("would you like to answer some questions \n choose y/n"))
quiz = quiz.lower()
while quiz != 'y' and quiz != 'n': //here you are using != that will result false this logic works fine with !
//while quiz == 'y' or quiz == 'n': //will work for or
print("please choose 'y' or 'n'")
input("y/n?")
//this code will work
quiz = str(input("would you like to answer some questions \n choose y/n"))
quiz = quiz.lower()
while quiz != 'y' and quiz != 'n':
print("please choose 'y' or 'n'")
input("y/n?")
I hope this helps.
You may be interested in flow charts or Algorithms please follow this link: https://www.edrawsoft.com/explain-algorithm-flowchart.php
You may also be interested to learn about python more please follow this link: https://www.python.org/about/gettingstarted/
Thank you.
Input looks for an integer, raw input will receive a string. Try using
raw_input(y/n)
This should clear the ValueError