Python Forum

Full Version: Seemingly simple loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So I literally just started learning python yesterday, I like to do projects to learn specific things cuz thats just how I learn. I know its probably not the best place to start but I decided to try making a text based explore game like zork. This is the code I have and that all works fine.
name = raw_input("Hello, What is your name? ")
player = name
prompt1 = raw_input("Would you like to play a game " + player + "?").lower()
if prompt1 == "yes":
    print("let's play!")
elif prompt1 == "no":
    print("You're no fun! Goodbye.")
else:
    print("yes or no?")
My question is how do I go about looping my else back up to my prompt1 so it asks the question again after an input thats neither "yes" or "no"? I believe what I need is a while true: with indentations of course but then I guess I just can't figure out how to make my else false. I will make the lets play into an input and continue as well but I made it print for this example

edit: thank you to the moderator for changing it. like I said I'm still new to this, I copied my script and it didn't work, tried to alter it and didn't change it. also looking at this I see nowhere to add a tag or that I needed to add that to the script
Here's one way to go about it but if you are going to control input throughout the whole game, I would consider writing a function that would accept a prompt and list or tuple of acceptable answers.

name = input("Hello, What is your name? ")
player = name
print ("Would you like to play a game " + player + "?")
prompt1 = 'BashBedlam was here.'
while prompt1 not in ('yes', 'no', 'y', 'n') :
	prompt1 = input ("yes or no?").lower ()
	if prompt1 == "yes":
		print("let's play!")
	elif prompt1 == "no":
		print("You're no fun! Goodbye.")
You should upgrade python.
You are using an obsolete version as 3.9.2 is current.
Thank you very much Bash, it's not exactly what I was looking for but it works perfectly and probably would be less work in the long run for a game filled with decision making. I guess I'm more curious now if looping back to the question is possible now. I would update it but unfortunately this is what I have as I'm sure some others do. Not on my main computer and am resorting to a very old computer
Python 2 support ended about a year and a half ago. I would be surprised (but it would not be the first time) if your computer did not support Python 3, at least 3.6 which seems to be the minimum supported by most libraries.

That said, you should also look at using dictionaries. Dictionaries can be used to map your responses. And, thinking more I would create a class called Room, where each room has attributes 'description' at least, a dictionary of where you go when you pick a direction, a list of items in the room that can be taken, etc.

Just thoughts.
(Mar-21-2021, 02:32 AM)Weber585 Wrote: [ -> ]I guess I'm more curious now if looping back to the question is possible now.
Something like this will loop back to whatever prompt you give it until the answer is within acceptable answers.

def get_acceptable_input (prompt: str, acceptable: tuple) -> str :
	answer = 'BashBedlam was here.'
	while answer not in acceptable :
		answer = input (prompt)
	return answer

print ('\n' * 7)
prompt = 'Please enter a number between 1 and 10 : '
acceptable_answers = ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10') 
answer = get_acceptable_input (prompt, acceptable_answers)
print (f'Your number is {answer}.')

print ()
prompt = 'Enter yes or not to contine : '
acceptable_answers = ('Yes', 'yes', 'Y', 'y', 'No', 'no', 'N', 'n')
answer = get_acceptable_input (prompt, acceptable_answers)
if answer [0].lower () == 'y' :
	print ("Okay then, Let's go!")
else :
	print ('Fine, be that way.')
My 2 cents
run = True
while run:
    name = input('What is your name? ')
    if name:
        print(f'Hello, {name.capitalize()}!')
        run = False
    else:
        print('\nPlease enter a name.\n')
        continue
Output:
What is your name? Please enter a name. What is your name? john Hello, John!
@menator01 - right, another reason to move to a more recent version of Python. f strings were not available in version 2.