Can't repeat what is asked in my code - python

I'm creating a text-based adventure game in Python 3.4.3 and I can't figure out how to make the code repeat a question. There's a bunch of narration before this, if that helps understand what's going on at all.
print("\n\n\n\n\nYou awake to find yourself in the center of a clearing in a forest.")
print("You stand up and decide to take a look around.")
str = input("Which direction do you steer your head? d= down, l= left, r= right, u= up, b= behind you: ")
print(" ")
if str in ("d"):
print("You see your combat boots and the grassy ground below your feet. ")
if str in ("l"):
print("The forest trees grow thicker and darker that way. You stare into the shadows and feel... cold...")
if str in ("r"):
print("The forest is warm and inviting that way, you think you can hear a distant birds chirp.")
if str in ("u"):
print("The blue sky looks gorgeous, a crow flies overhead... that's not a crow...")
print("It's a Nevermore, an aerial Grim. You stand still until it passes.")
if str in ("b"):
print("the grass slowly grows to dirt as the area falls into a mountain cliff. You now know where you are.")
print("Mount Glenn, one of the most Grim-infested places in all of Remnant.")
print("It's a bit unsettling.")
else:
print("Try that again")
I want the code to repeat the question to the user, until they've answered every possible answer and move on to the next question. I also want it to repeat the question when they get else. How do I do this?

Don't use str as a variable name, it will shadow an important builtin and cause weird problems.
Use a while loop to restrict the output to valid options.
valid_choices = ('d', 'l', 'r', 'u', 'b',)
choice = None
while choice not in valid_choices:
text = input("Which direction do you steer your head? d= down, l= left, r= right, u= up, b= behind you: ")
choice = text.strip()
if choice == 'd':
print ('...')
elif choice == 'u':
print ('...')
See also:
string.strip
tuples
None

Basically you can put your question in a loop and iterate through it until you enter one of the desired 'if' case. I have modified your code as below. Please have a look
print("\n\n\n\n\nYou awake to find yourself in the center of a clearing in a forest.")
print("You stand up and decide to take a look around.")
while True:
str = input("Which direction do you steer your head? d= down, l= left, r= right, u= up, b= behind you: ")
print(" ")
if str in ("d"):
print("You see your combat boots and the grassy ground below your feet. ")
break
if str in ("l"):
print("The forest trees grow thicker and darker that way. You stare into the shadows and feel... cold...")
break
if str in ("r"):
print("The forest is warm and inviting that way, you think you can hear a distant birds chirp.")
break
if str in ("u"):
print("The blue sky looks gorgeous, a crow flies overhead... that's not a crow...")
print("It's a Nevermore, an aerial Grim. You stand still until it passes.")
break
if str in ("b"):
print("the grass slowly grows to dirt as the area falls into a mountain cliff. You now know where you are.")
print("Mount Glenn, one of the most Grim-infested places in all of Remnant.")
print("It's a bit unsettling.")
break
else:
print("Try that again")

Do something like this:
answered = False
while not answered:
str = input("Question")
if str == "Desired answer":
answered = True

Here's how I would do this; explanation is in the comments:
# Print out the start text
print("\n\n\n\n\nYou awake to find yourself in the center of a clearing in a forest.")
print("You stand up and decide to take a look around.")
# Use a function to get the direction; saves some repeating later
def get_direction():
answer = input("Which direction do you steer your head? d= down, l= left, r= right, u= up, b= behind you: ")
print(" ")
return answer
# Keep running this block until the condition is False.
# In this case, the condition is True, so it keeps running forever
# Until we tell Python to "break" the loop.
while True:
# I changed "str" to "answer" here because "str" is already a Python
# built-in. It will work for now, but you'll get confused later on.
answer = get_direction()
if answer == "d":
print("You see your combat boots and the grassy ground below your feet. ")
# Stop the loop
break
elif answer == "l":
print("The forest trees grow thicker and darker that way. You stare into the shadows and feel... cold...")
break
elif answer == "r":
print("The forest is warm and inviting that way, you think you can hear a distant birds chirp.")
break
elif answer == "u":
print("The blue sky looks gorgeous, a crow flies overhead... that's not a crow...")
print("It's a Nevermore, an aerial Grim. You stand still until it passes.")
break
elif answer == "b":
print("the grass slowly grows to dirt as the area falls into a mountain cliff. You now know where you are.")
print("Mount Glenn, one of the most Grim-infested places in all of Remnant.")
print("It's a bit unsettling.")
break
else:
print("Try that again")
# NO break here! This means we start over again from the top
Now, none of this scales very well if you add more than a few directions;
because I assume that after you go "right" you want a new question, so that's a
new loop inside the loop, etc.
# The start text
print("\n\n\n\n\nYou awake to find yourself in the center of a clearing in a forest.")
print("You stand up and decide to take a look around.")
# Use a function to get the direction
def get_direction():
answer = input("Which direction do you steer your head? d= down, l= left, r= right, u= up, b= behind you: ")
print(" ")
return answer
# Use a function to store a "location" and the various descriptions that
# apply to it
def location_start():
return {
'down': [
# Function name of the location we go to
'location_foo',
# Description of this
'You see your combat boots and the grassy ground below your feet.'
],
'left': [
'location_bar',
'The forest trees grow thicker and darker that way. You stare into the shadows and feel... cold...'
],
'right': [
'location_other',
'The forest is warm and inviting that way, you think you can hear a distant birds chirp.'
],
'up': [
'location_more',
"The blue sky looks gorgeous, a crow flies overhead... that's not a crow...\n" +
"It's a Nevermore, an aerial Grim. You stand still until it passes."
],
'behind': [
'location_and_so_forth',
"The grass slowly grows to dirt as the area falls into a mountain cliff. You now know where you are.\n" +
"Mount Glenn, one of the most Grim-infested places in all of Remnant.\n" +
"It's a bit unsettling."
],
}
# And another location ... You'll probably add a bunch more...
def location_foo():
return {
'down': [
'location_such_and_such',
'desc...'
],
}
# Store the current location
current_location = location_start
# Keep running this block until the condition is False.
# In this case, the condition is True, so it keeps running forever
# Until we tell Python to "break" the loop.
while True:
# Run the function for our current location
loc = current_location()
answer = get_direction()
if answer == ("d"):
direction = 'down'
elif answer == ("l"):
direction = 'left'
elif answer == ("r"):
direction = 'right'
elif answer == ("u"):
direction = 'up'
elif answer == ("b"):
direction = 'behind'
else:
print("Try that again")
# Continue to the next iteration of the loop. Prevents the code below
# from being run
continue
# print out the key from the dict
print(loc[direction][1])
# Set the new current location. When this loop starts from the top,
# loc = current_location() is now something different!
current_location = globals()[loc[direction][0]]
Now, this is just one way of doing it; one downside here is that you'll need
to repeat the descriptions for the locations if you want to allow the player to
approach one location from different directions. This may not apply to your
adventure game (the original adventure doesn't allow this, if I remember
correctly).
You can fix that quite easily, but I'll leave that as an exercise to you ;-)

Related

How can I stop the loop after max wrong answer?

This is an exercise from Introduction to CompSci and Programming in Python from OCW MIT. I modified it a little bit. The problem is I want to stop the program when I reached the max wrong answer but it stops if I try two times. Why two times ? How can I fix this ? As a new starter should I ask every question here ? Thanks to all
n = 0
max_guesses = 3
n = input("You are in the Lost Forest\n****************\n****************\n :)\n****************\n****************\nGo left or right? ")
if n == "left" or "Left":
print("You're out of lost forest")
while n == "right" or n == "Right":
n = input("You are in the Lost Forest\n****************\n****** ***\n :(\n****************\n****************\nGo left or right? ")
n =+ 1
for n in range (max_guesses):
break
print("Game over! You ran out of your lives")
There are a few errors that contribute to your code not working properly
Your variable n is the same as the value you get from the value you input best to separate those into different values to make it easier to avoid hard to detect errors
You need to have a condition that checks if n >= max_guesses.
Welcome to the community, we try to discuss the issues here rather than looking for a whole solution.
I suggest you to first learn the very basics of programming like the decision making, control flows (i.e if statements and loops).
Also, try to attempt the question, multiple numbers of times with different logics and inputs at runtime. it will give you a better understanding of logical analysis.
Here's one of the ways you could have added the logic.
#Maximum allowed guesses
max_guesses = 3
def func1():
#Input string 'left' or 'right'
in_str = input("You are in the Lost Forest\n****************\n****************\n :)\n****************\n****************\nGo left or right? ")
#Number of attempts made
n = 1
if in_str.lower() == "left":
print("You're out of lost forest")
while in_str.lower() == "right":
in_str = input("You are in the Lost Forest\n****************\n****** ***\n :(\n****************\n****************\nGo left or right? ")
n += 1
if in_str.lower() == "left":
print("You're out of lost forest")
break
if n >= max_guesses:
print("Game over!!!")
break
print("Total attempts made by user: ",n)
func1()
I have attempted to fix your code however, I have had to remove the for loop and change the variable names. Hopefully, it helps.
n = 1
max_guesses = 3
a = input("You are in the Lost Forest\n****************\n****************\n :)\n****************\n****************\nGo left or right? ")
if a.lower() == "left":
print("You're out of lost forest")
while a.lower() == "right":
if n == 3:
print("Game over! You ran out of your lives")
break
else:
a = input("You are in the Lost Forest\n****************\n****** ***\n :(\n****************\n****************\nGo left or right? ")
n += 1

Problems with a Python if-else statement

I am doing an assignment for an IT class. Very basic Python projects. I have no experience with it but am able to grasp what is going on most of the time. This project involves making choices involving random integer selections. I can get the options to work the way I want in the if-else format but the else command isn't responding the way I want. The options all load properly but if the choice is 1-3 for some reason the program then prints the "else" statement after printing the "if" statements. This doesn't happen if they choose option 4 or pick an invalid number(the reason for the else statement).
I didn't have this issue in the previous section of the program. I must have missed something, but I can't tell what it is. I should reiterate I am very much a beginner at this and am basically copy-pasting code as the assignment instructed, and then editing it to suit my needs.
interactions = ["Back Away Slowly","Point Towards the Chamber","Attack","Try To Communicate",]
import random
badchoice = random.randint(1,3)
loop = 1
while loop == 1:
choice=menu(interactions, "How do you decide to interact?")
print("")
if choice == 1:
print("You start to slowly back away from the man.")
print("You worry you are giving off the wrong attitude.")
print("")
if choice == badchoice:
loop=0
print("The stranger was already weary of your presence.")
print(interactions[choice-1], "was not the correct decision.")
print("He calls for help. Villagers and guards immediately surround you.")
print("You are thrown back into the chamber. Hitting your head, and getting knocked unconscious in the process.")
print("You are back where you started and the items have been removed.")
print("There is no escape.")
print("Lamashtu's Curse can not be broken.")
print("Game Over.")
else:
print("The stranger looks at you in confusion.")
print("")
# Choices 2 and 3 go here. Same code. Seemed redundant to post all of it.
if choice == 4:
loop=0
print("The stranger is weary of your presence, and can not understand you.")
print("You can see in his eyes that he wants to help,")
print("and he escorts you back to his home for the time being.")
print("He offers you some much needed food and water.")
print("You are no closer to understanding what curse brought you here.")
print("Perhaps tomorrow you will have more time to inspect your surroundings.")
print("In time you may be able to communicate enough with your host to explain your dilemma,")
print("but for now you are trapped 6000 years in the past with no other options.")
print("At least you've made it out alive thus far......")
print("To be continued...")
else:
print("Please choose a number between 1 and 4.")
The else only applies to the most recent if at the same level, ie the one for number 4. The other three if statements are all independent. So after checking each of those, it then goes to check if the choice is 4, and runs the else if it isn't.
You need to make these all part of the same statement, which you do by using elif for every if after the first. Like this:
if choice == 1:
...
elif choice == 2:
...
elif choice == 3:
...
elif choice == 4:
...
else:
...
Your logic is incorrect. Your final if statement directs choice 4 to it's True branch, and everything else to the False branch. Python does exactly what you told it to do. There is no logical reference to the previous blocks.
The logic you need is
if choice == 1:
...
elif choice == 2:
...
elif choice == 3:
...
elif choice == 4:
...
else:
print("Please choose a number between 1 and 4.")

How can I make "two questions in one" in python?

So Im making like a quiz sort of adventure game in Python. And as you can see, at the very start I have a guess=input statement. And then I have all my if statements. And so I said for the first line of it when you press A, it will print that text out. And the text says to say another answer. However, it stops me from entering anything else after I type in A. how can i resolve this so I'm able to say more than 1 word?
print('\n')
if guess=input ("You drive to your house, and you notice that the doors are all unlocked and some chairs are flipped over. You also hear some footsteps and hammering in the guest room. What do you do? A: Go in the room and see what is going on, B: Yell you are calling the cops and hide C: Grab a knife and head in the room D: Get too scared and run away out the door ").strip()
if guess.upper() == "A":
print("Maybe not the wisest choice... He is there with a loaded gun aimed at you. What do you do? E: Panic and just sob and close your eyes F: Attempt a ninja move and try to kick him G: Try to distract him and then push him from the behind so he falls on the ground H: Try to talk him out of aiming the gun at you")
if guess.upper() == "E":
print("Not the best idea, he shot you and you died.")
return
if guess.upper() == "F":
print("It worked! he was thinking about if he really wanted to do this and got distracted. You grabbed him by the hands then tied him up behind his back on the ground and called the cops. He was arrested. ")
print("Turns out he was most wanted! Now you have 2 options. I: Get $200,000 now from the goverment for capturing a most wanted man, or J: Get $700,000 in 2 years from the goverment for capturing a most wanted man. ")
if guess.upper() == "I":
balance = balance + 200000
print("If you don't need the money right now because you aren't struggling, than why did you do this? you could get more than 2x more by waiting!")
if guess.upper() == "J":
print("Smart option! no gains now but you will get a way bigger gain in 2 years! as long as you weren't struggling financially this was a great option.")
if guess.upper() == "B":
print("You made him madder and he did not want to get involved with the police, so he shot you, you died.")
return
if guess.upper() == "C":
print("You got lucky! he did not notice you, you stabbed him and he died. You called the cops and they gave you a reward because he was most wanted. They gave you two options for a reward. K: Audi R8 spider right now, or L: Batmobile in 10 years.")
if guess.upper() == "K":
print("You got a Audi R8 right now! it is a very nice car, but you already have one and you would have the coolest one ever in 10 years!")
if guess.upper() == "L":
print("Nice choice. You realized you already had a car so you would get a amazing one in 10 years.")
if guess.upper() == "D":
print("This did not work. He saw you running and shot you from a ways away, you died.")
return
print("Total Balance:$",balance)
keepgoing = False
This was answered by #cᴏʟᴅsᴘᴇᴇᴅ:
def start():
guess = input("You drive to your house, and you notice that the doors are all unlocked and some chairs are flipped over. You also hear some footsteps and hammering in the guest room. What do you do? A: Go in the room and see what is going on, B: Yell you are calling the cops and hide C: Grab a knife and head in the room D: Get too scared and run away out the door ").strip()
if guess.upper() == "A":
guess = input("Maybe not the wisest choice... He is there with a loaded gun aimed at you. What do you do? E: Panic and just sob and close your eyes F: Attempt a ninja move and try to kick him G: Try to distract him and then push him from the behind so he falls on the ground H: Try to talk him out of aiming the gun at you")
if guess.upper() == "E":
print("Not the best idea, he shot you and you died.")
return
elif guess.upper() == "F":
guess = input("It worked! he was thinking about if he really wanted to do this and got distracted. You grabbed him by the hands then tied him up behind his back on the ground and called the cops. He was arrested.\nTurns out he was most wanted! Now you have 2 options. I: Get $200,000 now from the goverment for capturing a most wanted man, or J: Get $700,000 in 2 years from the goverment for capturing a most wanted man. ")
if guess.upper() == "I":
balance = balance + 200000
print("If you don't need the money right now because you aren't struggling, than why did you do this? you could get more than 2x more by waiting!")
elif guess.upper() == "J":
print("Smart option! no gains now but you will get a way bigger gain in 2 years! as long as you weren't struggling financially this was a great option.")
elif guess.upper() == "B":
print("You made him madder and he did not want to get involved with the police, so he shot you, you died.")
return
elif guess.upper() == "C":
guess = input("You got lucky! he did not notice you, you stabbed him and he died. You called the cops and they gave you a reward because he was most wanted. They gave you two options for a reward. K: Audi R8 spider right now, or L: Batmobile in 10 years.")
if guess.upper() == "K":
print("You got a Audi R8 right now! it is a very nice car, but you already have one and you would have the coolest one ever in 10 years!")
elif guess.upper() == "L":
print("Nice choice. You realized you already had a car so you would get a amazing one in 10 years.")
elif guess.upper() == "D":
print("This did not work. He saw you running and shot you from a ways away, you died.")
return
start()
I removed def main from it at the start because I already had def main and then it worked. Appreciate it and thanks for the help guys!
This is the answer.
balance = 0
print('\n')
guess=input ("You drive to your house, and you notice that the doors are all unlocked and some chairs are flipped over. You also hear some footsteps and hammering in the guest room. What do you do? A: Go in the room and see what is going on, B: Yell you are calling the cops and hide C: Grab a knife and head in the room D: Get too scared and run away out the door ").strip()
if guess.upper() == "A":
guess1 = input("Maybe not the wisest choice... He is there with a loaded gun aimed at you. What do you do? E: Panic and just sob and close your eyes F: Attempt a ninja move and try to kick him G: Try to distract him and then push him from the behind so he falls on the ground H: Try to talk him out of aiming the gun at you")
if guess1.upper() == "E":
print("Not the best idea, he shot you and you died.")
elif guess1.upper() == "F":
print("It worked! he was thinking about if he really wanted to do this and got distracted. You grabbed him by the hands then tied him up behind his back on the ground and called the cops. He was arrested. ")
guess2 = input("Turns out he was most wanted! Now you have 2 options. I: Get $200,000 now from the goverment for capturing a most wanted man, or J: Get $700,000 in 2 years from the goverment for capturing a most wanted man. ")
if guess2.upper() == "I":
balance = balance + 200000
print("If you don't need the money right now because you aren't struggling, than why did you do this? you could get more than 2x more by waiting!")
elif guess2.upper() == "J":
print("Smart option! no gains now but you will get a way bigger gain in 2 years! as long as you weren't struggling financially this was a great option.")
elif guess.upper() == "B":
print("You made him madder and he did not want to get involved with the police, so he shot you, you died.")
elif guess.upper() == "C":
guess3 = input("You got lucky! he did not notice you, you stabbed him and he died. You called the cops and they gave you a reward because he was most wanted. They gave you two options for a reward. K: Audi R8 spider right now, or L: Batmobile in 10 years.")
if guess3.upper() == "K":
print("You got a Audi R8 right now! it is a very nice car, but you already have one and you would have the coolest one ever in 10 years!")
elif guess3.upper() == "L":
print("Nice choice. You realized you already had a car so you would get a amazing one in 10 years.")
elif guess.upper() == "D":
print("This did not work. He saw you running and shot you from a ways away, you died.")
print("Total Balance:$", balance)
keepgoing = False
You need a while loop which is always true and then try to make a condition which your code escapes the while loop. Your code checks if an statement is true, in your case guess.upper()=A and then runs the code within the if statement and skips the rest of the code. Let's say you ask the user whenever you enter Z, the program ends. You can rewrite your code as:
While true:
{your code}
If guess.upper=='Z':
Break
Your second line is also wrong. What exactly do you want to do over there?

Python Black Jack game variant

My Black Jack code is very basic but is running quite smoothly, however I have run into a speed bump. Thus im here. When I call "Hit" to send me another card in my While loop, for every loop the DECK instantiates the same card. The first 2 drawn and the Hit card are always different but within the While loop (which is set to end when the player says "stay" and doesnt want another card.) the Hit cards remain the same.
import random
import itertools
SUITS = 'cdhs'
RANKS = '23456789TJQKA'
DECK = tuple(''.join(card) for card in itertools.product(RANKS, SUITS))
hand = random.sample(DECK, 2)
hit = random.sample(DECK, 1)
print("Welcome to Jackpot Guesser! ")
c = input("Would you like to play Black Jack or play Slots? ")
if c == 'Black Jack':
print()
print("Welcome to Black Jack! Here are the rules: ")
print("Black Jack is a game of whit, skill with a bit of Luck. You will start with 2 card and try to achieve a total of 21 points. \n Each cards worth is equal to its number, face cards are worth 10 and the Ace can be 1 or 11 points. You decide. \n You can decide to -Stay- and take the points you have or -Hit- to get another card. If you get 21 its Black Jack and you win. \n If no one gets 21, the highest points win, but if you go over 21 you -Bomb- and lose everything. \n Becarful not to Bomb and goodluck out there! Remember, you got to know when to Hit, when to Stay and when to walk away! \n")
print(hand)
print()
g = 'swag'
while g != 'Stay':
g = input(("What would you like to do, Stay or Hit: "))
if g == 'Hit':
print(hit)
elif g == 'Stay':
print("Lets see how you did!")
else:
print("test3")
elif c == 'Slots':
print("test")
else:
print("test2")
EX: Hand: Td(two diamonds), 3c(3 club)
Hit: 7s(7 spades)
hit 7s
hit 7s
hit 7s
...
stay: lets see how you did. I need the continued While loop Hits to differ, any ideas.
The problem is that you are generating the hit card only once, during the start of the program. Changing your code from
if g == 'Hit':
print(hit)
to something like
if g == 'Hit':
hit = random.sample(DECK, 1)
print(hit)
will make it outputs different cards on each hit.

how to loop back to certain code

So I am creating a survival game, and I need to know how to loop back to a certain point in the code. I have wrapped the entire game in a function, but--now when in run it-- it just restarts itself.
import random
def game(choice1):
print "You need to build a fire. the recipe is 5 stick and 3 coal."
choice1 = raw_input("There are trees to your left and rocks to your right. Which way will you go?")
if choice1 == "left" or choice1 == "Left":
choice2a = raw_input("You go to the tree. Would you like to punch it?")
if choice2a == "yes" or choice2a == "Yes":
R = random.randint(1,11)
print "You punched the tree " + str(R) + " times."
if R <= 5:
print "It did not fall down"
elif R > 5:
R2 = random.randint(0, 5)
print"It fell down. It dropped " + str(R2) + " sticks."
elif choice2a == "no" or choice2a == "No":
game(choice1)
if choice1 == "right" or choice1 == "Right":
choice2b = raw_input("You go to the rocks. Would you like to pick up the coal in them?")
return game(choice1)
I imagine you want to loop back to the first raw_input statement, if that is the case, you can use while loops as Celeo has pointed above.
You will have to use an exit condition to escape the while loop once you are done looping.If you want to loop back to different parts of the program then I would recommend you write the code block as functions and call them as necessary.
Try reading about while loops here:
http://www.tutorialspoint.com/python/python_while_loop.htm
A while loop lets you repeat a code depending on a condition (while a condition is met, go back to the start).
To make such a game I would recommend using a (decision) state-machine. If the game is in a certain state, the player is asked the corresponding question and depending on the answer the game is moved to an other state. Each state should be implemented independently from the others, i.e. avoid deeply nested if/else constructs, this avoids errors and helps you to stay on top of things. You can also visualize/draw the game decision plan as a graph, where each node represents a state (or decision to be made) and each decision connects the node to an other state.
For your concrete example, you also need to keep track of what the player has collected so far, which is essentially an other state-system.
To implement such a state-based-game you can (ab-)use the python concept of generators. A generator is basically an object that returns 'items' with yield when queried with next(). A generator can provide a finite or an infinite amount of 'items', it can also return 'items' from an other generator with yield from.
Here an example implementation of your game:
Be aware that this code works only with python3! It should be possible to translate it into python2 (perhaps even automatically), but I feel no strong urge to do so ATM ;)
import random
import collections
def main(backpack):
print("You need to build a fire. the recipe is 5 stick and 3 coal.")
print("You have the following items in your backpack:")
for k,v in backpack.items():
print(' % 3d %s' % (v,k))
#TODO: add check if we have collected enough here
yield from choice1(backpack)
# tree or stone
def choice1(backpack):
answer = input("There are trees to your left and rocks to your right. Which way will you go?")
if answer.lower() in ('l', 'left'):
yield from choice2a(backpack)
elif answer.lower() in ('r', 'right'):
yield from choice2b(backpack)
else:
print('I could not understand you. Answer with either "left" or "right".')
yield from choice1(backpack)
# punch or not
def choice2a(backpack):
answer = input("You go to the tree. Would you like to punch it?")
if answer.lower() in ('y', "yes"):
R = random.randint(1,11)
print( "You punched the tree " + str(R) + " times.")
if R <= 5:
print("It did not fall down")
else:
R2 = random.randint(0, 5)
print("It fell down. It dropped " + str(R2) + " sticks.")
backpack['stick'] += R2
yield from choice2a(backpack)
elif answer.lower() in ('n', "no"):
yield from main(backpack)
else:
print('I could not understand you. Answer with either "yes" or "no".')
yield from choice2a(backpack)
# pick up or not
def choice2b(backpack):
answer = input("You go to the rocks. Would you like to pick up the coal in them?")
# TODO: implement this
yield main(backpack)
if __name__ == '__main__':
backpack=collections.defaultdict(int)
while True:
next(main(backpack))
Each of the functions is a generator-function, i.e. a function that returns a generator. Each of them represents a game-state that requires a decision. The player-state (i.e. what the player has collected so far) is passed along as backpack which is a dictionary that contains the amount per item.
(yield from xyz() could be interpreted as a kind of goto command.)

Categories

Resources