Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your code for this has some serious structural problems. If I understand correctly, you're trying to accept repeated commands and execute some code to make them function the way you intend.</p> <p>The problem is that your function to run the game is recursive, so every time you execute a command other than 1, 2, 3, or 4, you're calling your function again without returning from the first one. Eventually, if you enter enough commands, you'll get an error saying that you're recursing too deeply and the game will error out.</p> <p>What you want is something more like this:</p> <pre><code>def prompt(): x = input('Type a command: ') return x def ProcessAction(command): if command == '1': print() elif command == '2': print() elif command == '3': print() elif command == '4': print() elif command == 'geld': #Actions start here print('\n\tYou have ' + str(gold) + ' euro. RICH BOY BRO!.\n') print() elif command == 'inv': if not inv: print("\n\tYou don't have any items..\n") else: print('\n\t' + str(inv) + '\n') #Actions end here curr_command = None while curr_command not in ("1", "2", "3", "4"): curr_command = prompt() ProcessAction(curr_command) </code></pre> <p>What this will do is keep asking for new commands and processing them until one of the commands that exits the game is entered.</p> <p>Edit: From your comment below, it sounds like you're trying to figure out how to display gold and inventory every time a command is entered without requiring a special command to do it. If this is what you're after, you can add print statements to the <code>while</code> loop above to ensure that it's printed before every prompt. In that case, the while loop might look like:</p> <pre><code>while curr_command not in ("1", "2", "3", "4"): print('\n\tYou have ' + str(gold) + ' euro. RICH BOY BRO!.\n') if not inv: print("\n\tYou don't have any items..\n") else: print('\n\t' + str(inv) + '\n') curr_command = prompt() ProcessAction(curr_command) </code></pre> <p>Hope that gets closer to what you're after.</p> <p>Edit 2: Ok, after reading the full code for your game, I think you might want to consider reorganizing the entire thing. Think about what you want the game to be: the player enters a series of commands and each command does two things, it changes the game's state and it prints out a response based on both the current state and the new state.</p> <p>So, you should think about processing your commands with a loop like I described. Then, fold all those different functions into ONE <code>ProcessAction(command)</code> function that figures out from the game's state (which you store in variables) what to print out and how to change the state.</p> <p>If it's a game where you're going room to room, for example, you might keep a global variable <code>room</code> that defines where you are. Your <code>ProcessAction</code> function then follows logic that says "If I'm in room A and the character types this thing then print out B and change room to C, and sets gold to 0."</p> <p>To make this work well, you'll have to step back and think about the overall "story" of your game, how to store the state in various variables, and how to make your ONE <code>ProcessAction</code> function handle all the possible states and commands that can be issued.</p> <p>Doing this puts you on the path of developing what's called a "state machine," where you have a simple, general function that looks at a data structure (probably some nested dicts) that you fill up with what each command does when your game is in each state and where to go next, as well as what to print out.</p> <p>This Wikipedia article describes the concept of a state machine. How you implement it in Python is up to you. I can tell you that if you're careful you should be able to do this without repeating any code. <a href="http://en.wikipedia.org/wiki/State_machine" rel="nofollow">http://en.wikipedia.org/wiki/State_machine</a></p> <p>Another edit: Answering a question you placed in the comments below, if you think you have to print out, for example, the value of a player's gold in multiple places, you can do something like this:</p> <pre><code>def print_gold(gold_value): print('\n\tYou have ' + str(gold_value) + ' euro. RICH BOY BRO!.\n') print() </code></pre> <p>then use <code>print_gold(gold)</code> in place of those print statements whenever you need to do that. However, I think you may want to take a step back and think about rewriting the whole thing with some of the thoughts I've offered before you tackle that problem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload