How to return to beginning of loop? - python

search_room = raw_input("Search the:")
if search_room == "classroom":
print "You quitly make your way to the classroom, which is empty besides last night's homework"
sleep(3)
print "Enter 'inspect' into the command line to inspect the homework, or enter exit, to return to the kitchen."
action = raw_input("Command:")
if action == "inspect":
print "Hmph, uncompleted. Thats strange."
print "Enter 'inspect' into the command line to inspect the homework, or enter exit, to return to the kitchen."
action = raw_input("Command:")
if action == "exit":
print "you return to the kitchen"
search_room = raw_input("Search the:")
if action == "classroom":
I'm trying to figure out how to have this loop so i one could travel back and forth between the kitchen and the classroom, but if I try to go back to the classroom after exiting, I get an error message regarding a later 'for loop'.

If I understand your question correctly, your trying to figure out how to set up a loop that will let you go from kitchen back to classroom and so on and so forth without needing to nest an infinite amount of conditionals. I suggest you have an outer while(true) loop power the sequence of steps, and add conditions to check if a move is valid (to go to to the kitchen you must currently be in the classroom and have entered exit, ect)

You need to put the code block inside its own loop with a simple boolean sentinel value (while not stop) and if stop is set to true at any point, the otherwise endless loop will stop. Also remember the importance of python indentations for distinguishing code scopes, different from languages like java where it doesn't matter where you put the code as long as it is between the { }. Also consecutive if statements will each be executed individually unless you use if..elif which is what I think you want because you are comparing the same variable in the same scope.
Also if you have more than two rooms, like something in the game Clue, you should define some sort of scoping for each room. If you are in a room, which rooms can you access from there? I did this just with a simple dictionary scopes where each room is associated with a list of rooms it can access which is verified on getting search_room. They only have access to each other right now, but you can add more to each list to increase scope to other rooms (and add more if branches)
stop = false;
scopes = {"kitchen": ["classroom"], "classroom": ["kitchen"]}
locCur = "kitchen" #starting room
locPrev = "kitchen"
while not stop:
search_room = raw_input("Search the:")
if scopes[locCur].index(search_room) > -1 : #validate the room choice
if search_room == "classroom":
locCur = "classroom" #update current scope
print "You quietly make your way to the classroom, which is empty besides last night's homework"
sleep(3)
print "Enter 'inspect' into the command line to inspect the homework, or enter exit, to return to the kitchen."
action = raw_input("Command:")
if action == "inspect":
print "Hmph, uncompleted. Thats strange."
print "Enter 'inspect' into the command line to inspect the homework, or enter exit, to return to the kitchen."
action = raw_input("Command:")
if action == "inspect":
#print/do something else
elif action == "exit":
locCur = locPrev #set scope back to what it was
print "you return to the " + locPrev
locPrev = "classroom" #store this location
continue
elif search_room == "kitchen":
#do something for this room
print "message"
continue
elif search_room == "some other room":
#do something for this room
continue
elif search_room == "stop":
stop = true

Related

How do I get my code to run the if statement?

I'm a beginner in Python and I'm writing a code for a school project and ran into an early bug.
For some reason my if function won't run.
import time #imports computer time to program(buit in function)
count= 0
print(" Gymship") # center this
print("--------------------------------------") # this should go across the whole screen
print("Input a level to view the description or InputSign up to begin signing up for a card")
print("--------------------------------------------------------------------------")
print("Bronze")
time.sleep(1) # this wil pause the program for 1 second(for effect)
print("Silver")
time.sleep(1)
print("Gold")
time.sleep(1)
print("Platinum")
time.sleep(2)
print("-----------------------------------------------") # this should go across the whole screen
print("Sign up")
print(" ")
input()
if input == "Bronze":
print("Bronze")
print("--------------------------------------------")
print("You acquire a bronze card when you use two or less gym services")
print("2 Hours limit in the gym")
print("-------------------------------------")
print(input("Back to return to menu screen"))
count = count + 1
This is not correct:
input()
if input == "Bronze":
The way input() works is by returning a value. The name input refers to the function itself, so the function input will never equal the text "Bronze" unless you explicitly do something bad, like input = "Bronze" (it's bad because if you overwrite input, you'll no longer be able to access that function).
Instead, you should be using the returned value:
usr_input = input()
if usr_input == "Bronze":
Also, the line print(input("Back to return to menu screen")) is unnecessarily complicated; the print() will print whatever was returned by input(), but input() will display the "Back to return to menu screen" prompt without wrapping it in an if statement. So, input("Back to return to menu screen") is all you need. If you keep it the way you have it, if someone typed some text and then hit enter, the text would display again, because the print() is printing whatever that text was that the user typed.
You first need to assign a variable to the input and then check if the variable is equal to "Bronze"
Right now you are taking the input, but are not storing it anywhere. So the fixed code would be
user_input = input()
if user_input == "Bronze":

I have nested functions in a submenu that are not working as I want it to

I have a menu for booking reservations and it writes a reservation list and seating chart to text files every time someone enters a new reservation.
I have a menu that has the option to display the seating chart, purchase a seat, a nested admin menu, or quit. the admin command opens a new menu that lets you clear the contents of the reservation list, return to the main menu, or quit; or at least it is supposed to. I must have something wrong with the error checking, or maybe I cannot build it this way. Can someone see where i am going wrong? I have a large program and can post more, but would rather just post the portions i am having trouble with. the other functions are defined at the main program level(same level as the menu function), should the nested functions also be defined at that level? thanks.
def menu():
print(
'Select chioce from menu:\n\nP to purchase an available bus seat\nD to display current bus seat chart\nEnter password (admin) for admin menu\nQ to quit')
while True:
try:
menu_choice = str(input(' \n')).upper()
if menu_choice == str('D'):
display()
break
elif menu_choice == str('P'):
book()
break
elif menu_choice == str('ADMIN'):
def clear_booking(): # the nested functions are not working-
print('Clearing bookings')
with open("bookings.txt") as f:
f.write("")
def previous():
menu()
print('CB to clear bookings\nR to return to main menu\nQ to quit')
admin_choice = str(input(' \n')).upper
if admin_choice == str('CB'):
clear_booking()
elif admin_choice == str('R'):
previous()
elif admin_choice == str('Q'):
exit()
else:
print('Invalid choice')
exit()
elif menu_choice == str('Q'):
print('exiting program')
exit()
else:
print('Value not recognized.')
exit()
except ValueError:
print('Invalid entry')
return # how do i get rid of the none from this returning correctly?
print(menu())
You forgot the () at the admin_choice = str(input(' \n')).upper at the upper() function call.
Also maybe you can return a string with text instead of just return at the last line of your menu() function body
Also try to include your problem statement in the introductory text of your question to make it clear what the problem is with your code, and what you want to achieve.

Function called without being told to

Newish to python, working on a text adventure, testing out the use of functions.
def cell1():
loop = 1
while loop == 1:
print("ONE")
cave1 = input()
if cave1 == ("end?"):
print("\nthis should end program")
loop = 0
break
elif cave1 == ("TWO"):
global testvar
testvar = 1
option1()
else:
print("INVALID")
def option1():
print("TWO")
loop = 1
while loop == 1:
print("test1 definition")
print (testvar)
test1 = input()
if test1 == ("ONE"):
print("you pick up the cheese")
loop = 0
cell1()
elif test1 == ("THREE"):
option2()
else:
print("INVALID")
def option2():
print("THREE")
loop = 1
while loop == 1:
print("This is option 3")
test2 = input()
if test2 == ("ONE"):
print("testering2")
cell1()
elif test2 == ("TWO"):
global testvar
testvar = 2014
option1()
else:
print("INVALID")
run = True
while run == (True):
print ("testing 123")
cell1()
print("restart about to activate")
cont = input("Restart? ")
if (cont) != "yes":
break
This program should allow you to go between options (what would be rooms) and eventually in cell1, the program should be end-able.
if the program is run and "end?" is typed as the first input, the program goes into the continue bit at the bottom, however, if you go between the 'rooms' then back to cell1, typing "end?" will call option 2.
Ive had a look around and it is still baffling me, am i ding something wrong?
Any help is appreciated, thank you.
The reason "end?" only quits for the player when they are within the first cell is because you're only checking for that input therein. The execution contained within option1() and option2() doesn't affect the execution of cell1(). You're not returning anything from your option functions, nor are you changing a sentinel value.
So, there's two basic ways you could go about this.
First, you could return a value from your functions:
if option1() == "END":
break
Or, you could alter your while loop:
# is_running is defined globally
while is_running:
And then just set is_running to False in any of your methods whenever the user types "end?". That'd probably be the easiest way with the design you're using now.
I'm sure you can tell, though, that in general your program is going to get exponentially more complex as you add more rooms and your function calls get further nested.
I'm pretty sure that the issue you're having is because you don't always break out of the loop in one function when you call another function. For instance, if your entries were TWO, ONE then end?, you'd find yourself still in the cell1 loop. That's because when the inner call to cell1 returns, the control flow of the program goes back to where that function was called from, which is option1, since loop is now 0, the loop ends and option1 returns, to the outer call to cell1, where the loop is still running.
Unless you want the game you're designing to have a tree structure, where you can return to where you came from with different semantics than moving to some other place, I'd suggest using a different architecture. Rather than each of your functions calling the next function when appropriate, return that function instead. Then you'd write a single top level loop that calls the function. Here's an example where the function to be called by the top level loop is saved in a variable named state:
def cell1():
print("In cell1!")
while True:
choice = input("pick 'ONE' or 'TWO' (or type 'quit' to exit):")
if choice == "ONE":
return option1
elif choice == "TWO":
return option2
elif choice == "quit":
return None
else:
print("I'm sorry, I didn't understand that.")
def option1(): # these other two functions are very basic in my example
print("In option1!") # but you can make them as complex as you want
return option2
def option2():
print("in option2!")
return cell1
def control_loop(initial_state=cell1):
state = initial_state
while state is not None:
state = state() # the next state is the return value of the previous state
The problem is you are getting deeper and deeper within nested functions. For example, changing
if test1 == ("ONE"):
print("you pick up the cheese")
loop = 0
cell1()
to
if test1 == ("ONE"):
print("you pick up the cheese")
loop = 0
break
will allow you to run your program, enter room two, go back to room one, and "end?" will work properly. This won't fix your issues completely though because there is a similar problem where when you go from two to three where if you simply changed
if test2 == ("ONE"):
print("testering2")
cell1()
to
if test2 == ("ONE"):
print("testering2")
break
it would break the current function and go back into option1() (if you run your program, go to room two, then to room three, then back to one) where "end?" doesn't do anything. Hopefully this gets you on the right track.

changing response based on variable

I'm trying to figure out how to use an if statement before the assignment to change it is made. The point of this script is to check if the knife is taken before it prompts with the question to take it from the table. It's to make it so that you can walk back to the table and get another response if you've already taken it. What am I doing wrong?
def table ():
if knife_taken == False:
print "it's an old, brown wooden table, and atop it you find a knife"
print "Will you take the knife or go back?"
knife = raw_input ("> ")
if knife.strip().lower() in ["back", "b", "no"]:
basement2()
elif knife.strip().lower() in ["take knife", "knife", "yes", "k"]:
knife_taken = True
print "You now have the knife, good, you are going to need it"
raw_input()
basement2()
else:
print "I did not understand that."
raw_input()
table()
else:
print "There's nothing on the table"
raw_input()
basement2()
Basically when you change the variable knife_taken in your function you change it at a local level, this means when the function ends the changes are lost. There are two ways to fix this either use global (but thats the bad way)
global knife_taken
knife_taken = True
Or you can return the state of the knife from the function
return knife_taken
# later on
kitchen(knife_taken)
and store it in a variable, passing it back to kitchen later as an arguement
Or as an extra little bonus, you can store game state in a dictionary. You could then update the dictionary as the game state changes e.g.
game_state = {}
game_state['knife_taken'] = False
def kitchen():
if not game_state['knife_taken']:
print "Take the knife!"
game_state['knife_taken'] = True
else:
print "Nothing to see here."
kitchen()
kitchen()

How to have a versatile function call that can call different functions in Python?

I'm trying to make a text-based game in Python, however, code could get out of hand pretty quickly if I can't do one thing on one line.
First, the source code:
from sys import exit
prompt = "> "
inventory = []
def menu():
while True:
print "Enter \"start game\" to start playing."
print "Enter \"password\" to skip to the level you want."
print "Enter \"exit\" to exit the game."
choice = raw_input(prompt)
if choice == "start game":
shell()
elif choice == "password":
password()
elif choice == "exit":
exit(0)
else:
print "Input invalid. Try again."
def password():
print "Enter a password."
password = raw_input(prompt)
if password == "go back":
print "Going to menu..."
else:
print "Wrong password. You are trying to cheat by (pointlessly) guess passwords."
dead("cheating")
def shell(location="default", item ="nothing"):
if location == "default" and item == "nothing":
print "Starting game..."
# starter_room (disabled until room is actually made)
elif location != "default" and item != "nothing":
print "You picked up %s." % item
inventory.append(item)
location()
elif location != "default" and item == "nothing":
print "You enter the room."
location()
else:
print "Error: Closing game."
def location():
print "Nothing to see here."
# Placeholder location so the script won't spout errors.
def dead(reason):
print "You died of %s." % reason
exit(0)
print "Welcome."
menu()
First, an explanation on how my game basically works.
The game has a 'shell' (where input is done) which receives information from and sends information to the different 'rooms' in the game, and it stores the inventory. It can receive two arguments, the location and an eventual item to be added to the inventory. However, line 40-42 (the first elif block in 'shell') and line 43-45 (the last elif block in 'shell') are supposed to go back to whatever location the location was (line 42 and 45, to be exact). I've tried "%s() % location" but that doesn't work, it seems to only work when printing things or something.
Is there any way to do this? If not, even writing an engine for this game would be a nightmare. Or I'd have to make an entirely different engine, which I think would be a way better approach in such a case.
Sorry if I made any mistakes, first question/post ever.
elif location != "default" and item != "nothing":
print "You picked up %s." % item
inventory.append(item)
location()
elif location != "default" and item == "nothing":
print "You enter the room."
location()
I guess you want to call a function having its name. For that you need a reference to the module or class inside which it was defined:
module = some_module # where the function is defined
function = getattr(module, location) # get the reference to the function
function() # call the function
If the function is defined in the current module:
function = globals()[location]
function() # call the function
If I correctly understand what you want is something like this : player will enter a location name and you want to call the related method. "%s"()%location will not work, a string (that is what is "%s" is not callable).
Let's try an OOP way :
class Maze:
def __init__(self):
# do what you need to initialize your maze
def bathroom(self):
#go to the bathroom
def kitchen(self):
# go to the kitchen
def shell(self, location="", item=""):
if location == "" and item == "":
print "Starting game..."
# starter_room (disabled until room is actually made)
elif location and item:
print "You picked up %s." % item
inventory.append(item)
getattr(self, location)()
elif location and item == "":
print "You enter the room."
getattr(self, location)()
else:
print "Error: Closing game."
maze = Maze()
while True: # or whatever you want as stop condition
location = raw_input("enter your location :")
item = raw_input("enter your location :")
maze.shell(location=location, item=item)
I think you can use the getattr() method.
Example : You want to call method "helloword()" from module "test", you would then do :
methodYouWantToCall = getattr(test, "helloworld")
caller = methodYouWantToCall()
Hope it gives you a clue.

Categories