473,396 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Python word game, functions wont work?

I have been going through the book: learn python the hard way, and i have come to exercise 43, where you build your own 'game'. This is my attempt so far, all the functions are working, except for the death() function? why wont it work?

When i type: python game.py>admin>left it should say that i die, but it goes onto the "right" option as well?


here it is:

Expand|Select|Wrap|Line Numbers
  1. from sys import exit
  2. from random import randint
  3.  
  4.  
  5.  
  6.  
  7. def main_hall():
  8.     print "You are on the war ship. You need to get into the elevator to get to the bridge; where the General is."
  9.     print "The whole destroyer is on alert and nearly every door is locked. There is a keypad on the elevator."
  10.     print "There is a warning saying you only have 10 attemps, then the system deactivates and you cannot complete your mission. Be careful!"
  11.  
  12.     combination = "%d%d%d"  % (randint(1,9), randint(1,9), randint(1,9))
  13.  
  14.     guess = raw_input ("[Keypad:] ")
  15.  
  16.     guesses = 0
  17.  
  18.     while guess != combination and guesses < 10 and guess != "admin":
  19.         print "*BZZT!* ERROR 2355: Incorrect combination!"
  20.         guesses += 1
  21.         guess = raw_input ("[Keypad:] ")
  22.  
  23.     if guess == combination:
  24.         print "You open the elevator and start going up, fast. "
  25.         return 'junction'
  26.  
  27.     elif guess == "admin":
  28.         return 'junction'
  29.  
  30.     else:
  31.         print "You have had more than 10 attempts! \n A small self destruct system in the lock blows you up!\n"
  32.         return 'death'
  33.  
  34.  
  35.  
  36.  
  37. main_hall()
  38.  
  39.  
  40.  
  41. def junction():
  42.     print "When you get to the top, you are at a T-junction. Do you go left or right?"
  43.     action = raw_input ("> ")
  44.  
  45.     if action == "left":
  46.         print "You start running left, gun in hand, until you come across a turn, with some dark mist. You attempt to run through the mist, but you start to choke and you cannot get out. You die!"
  47.         return 'death'
  48.  
  49.     elif action == "right":
  50.         print "You start running down the right corridor, gun in hand. You finally make it to the brige where you must take control of the ship. You then reallize there is a crew of around 4-5 people. What do you do?"
  51.         return 'bridge'
  52.  
  53. def bridge():
  54.     action = raw_input ("Kill;\nPretend you're one of them?\nRun?\n> ")
  55.  
  56.     if action == "kill":
  57.         print "You click the safety off you rifle, and start opening fire. You manage to kill all but one of them. You try to shoot at the last man, but he is too fast! You see him initiate the self destruct! You die!"
  58.         return 'death'
  59.  
  60.     elif action == "pretend":
  61.         print "You walk into the bridge, casually. They dont make any signs of noticing at first, but then they see your uniform and start shouting questions at you. Then, they start shooting you. You start shooting back but it was too late. You die!"
  62.  
  63.     elif action == "run":
  64.         print "You start running back the way you came, until  you find somebody else from you ship. You tell him about what happens, you go back and complete the mission. Congratulations!"
  65.         return 'win'
  66.  
  67.  
  68. def win():
  69.     print "Horray! You take control of the ship, and become a hero!"
  70.     exit (1)
  71.  
  72. def death():
  73.     print "Well you really have failed this mission. GO HOME LOSER!"
  74.     exit (1)
  75.  
  76. junction()
  77. bridge()
  78. win()
  79. death()
Dec 24 '11 #1

✓ answered by dwblas

Do don't catch any of the returns. Change the last lines to
Expand|Select|Wrap|Line Numbers
  1. result = junction()
  2. if result != "death":
  3.     bridge()
  4.     win()
  5.     death() 

1 2321
dwblas
626 Expert 512MB
Do don't catch any of the returns. Change the last lines to
Expand|Select|Wrap|Line Numbers
  1. result = junction()
  2. if result != "death":
  3.     bridge()
  4.     win()
  5.     death() 
Dec 24 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Oliver Spiesshofer | last post by:
Hi, I have a script that calls an fopen() on an external URL. I can run the script from the server as an url fine. When however the script should be run from crontab, it does not work. I get ...
5
by: Catherine | last post by:
I am having a problem viewing asp pages on iis version 5.1 xp pro. HTML pages are viewable on http://localhost but .asp pages are not. I have created a test program called timetest.asp with the...
19
by: Allen Thompson | last post by:
sorry for the simple question, haven't done this in a while. when I use the following script it keeps displaying the value of "x" like a string. for example, if I type the number 7 in the prompt,...
0
by: Sebastian Sosna | last post by:
Hi there! iam trying a code snippet from MSDN heres the link : http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netdir/netds/creating_groups.asp ive tried the code but it...
7
by: Christine | last post by:
My code has a split function that should split the text file of numbers. I've run this in previous programs as it is here and it worked, but now it wont work for some reason and returns...
1
by: Gary | last post by:
I have the following in my web.config.... <customErrors mode="RemoteOnly" defaultRedirect="Applicationerror.aspx"/> but, my error page does not display. Instead a 'Runtime Error' page...
14
by: squash | last post by:
The following code works fine in Firefox/Netscape but wont work in IE. I suspect the problem is with one of these two simple functions. If there is no obvious error Ill paste the entire code. ...
8
by: Mark12345 | last post by:
Hi folks, thanks for looking. I have a database that takes down order details. Each customer has there own priceid in the price table. the errors that I get say enter parameter value for:...
2
by: jcollins1991 | last post by:
im trying to make a program thats supposed to check whether 2 strings are anagrams of each other (anagram = same letters in both strings, but in different orders... "cool as wet art" , and "cs at...
2
by: Admiral | last post by:
Hi, Anyone knows how this works? I have made a textbox called Editbox1 and 10 buttons. b0, b1, b2 etc. What i want to do is when i press a button, b1 for example, a number 1 appears into...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.