I wrote this code for a text-based adventure using Python 3.9. It checks the players answers afterward and keeps them from entering anything other than 1,2, or 3. After the player enters 1,2,or 3 then the code cuts off and does not progress to the rooms that I defined. I tried to figure out what's going on by moving things around, but I kept getting errors. Can anyone tell me why it isn't working? If the player enters K instead of the answer choice it prompts them to enter "1, 2 or 3" then once they enter the correct one it ends the program and does not progress them to the other rooms. I wanted to use the check_function to constantly check throughout the adventure if the player is entering 1, 2, or 3.
#KD
#Functions
def start_game():
player_choice=""
player_options = ["1", "2", "3"]
print("Starting dialogue")
player_choice= str(input ("Enter 1, 2, or 3 to continue"))
#print ("You chose:" + player_choice)
if player_choice==player_options [0]:
room01()
elif player_choice==player_options [1]:
room2()
elif player_choice==player_options [2]:
room3()
else:
check_choice(player_choice, player_options)
def check_choice(player_choice,player_options):
while player_choice not in player_options:
print ("---In Start Room")
player_choice= str(input ("Enter 1, 2, or 3 to continue"))
continue
#print()
def room01():
print("Welcome to room 1")
def room2():
print("Welcome to room 2")
def room3():
print("Welcome to room 3")
#main code
start_game()
I'm assuming you want to repeatedly prompt, instead of just ending when they answer the first time. If that's the case, try this:
def start_game():
player_choice=""
player_options = ["1", "2", "3"]
print("Starting dialogue")
while True:
player_choice= str(input("Enter 1, 2, or 3 to continue: "))
if player_choice not in player_options:
break
if player_choice==player_options[0]:
room01()
elif player_choice==player_options[1]:
room2()
elif player_choice==player_options[2]:
room3()
else:
check_choice(player_choice, player_options)
It'll repeat forever until they enter something that's not one of the options you defined (not 1 2 or 3)
When I copied and pasted the code in my terminal, the welcome message was displayed properly:
def room01():
print("Welcome to room 1")
def room2():
print("Welcome to room 2")
def room3():
print("Welcome to room 3")
def start_game():
player_choice = ""
player_options = ["1", "2", "3"]
print("Starting dialogue")
player_choice = str(input("Enter 1, 2, or 3 to continue"))
if player_choice == player_options[0]:
room01()
elif player_choice == player_options[1]:
room2()
elif player_choice == player_options[2]:
room3()
else:
check_choice(player_choice, player_options)
def check_choice(player_choice, player_options):
while player_choice not in player_options:
print("---In Start Room")
player_choice = str(input("Enter 1, 2, or 3 to continue"))
continue
# print()
# main code
start_game()
Output
Starting dialogue
Enter 1, 2, or 3 to continue>? 3
Welcome to room 3
Are you doing something different in your script?
I just tested your code and your condition and while loop seemed to work just fine. You could maybe add a bit more information like what OS you are using, what editor and terminal you are using and so on.
Only questions that pop to mind are:
With a bit more info I am sure we can help: :)
Result from your code on my machine:
The condition to prevent any other number is a bit buggy but everything else seems to work
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.