Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Seemingly simple loop
#1
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
Larz60+ write Mar-21-2021, 02:06 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts
When properly used indentation will show properly if there.
Reply
#2
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.")
Reply
#3
You should upgrade python.
You are using an obsolete version as 3.9.2 is current.
Reply
#4
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
Reply
#5
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.
Reply
#6
(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.')
Reply
#7
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!
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#8
@menator01 - right, another reason to move to a more recent version of Python. f strings were not available in version 2.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  why does VS want to install seemingly unrelated products? db042190 3 608 Jun-12-2023, 02:47 PM
Last Post: deanhystad
  Simple Variable Saving in Loop DevDev 3 2,960 Mar-09-2021, 07:17 PM
Last Post: Fre3k
  Seemingly unstable GPIO output while executing from RetroPie LouF 6 3,860 Feb-19-2021, 06:29 AM
Last Post: LouF
  simple for loop? gr3yali3n 3 2,482 Sep-22-2020, 05:35 AM
Last Post: buran
  Adding second message to simple loop error PythonGainz 2 2,055 Apr-06-2020, 11:55 AM
Last Post: PythonGainz
  simple key value loop help siten0308 4 2,390 Jun-25-2019, 02:53 PM
Last Post: siten0308
  Simple while loop only works on first attempt jsb83 2 1,991 Jun-20-2019, 08:57 PM
Last Post: jsb83
  .split seemingly gets ignored liquidsnake 8 3,936 Feb-22-2018, 04:46 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020