Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6239983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:29:07+00:00 2026-05-24T11:29:07+00:00

like the title says, I have a single bug in a script for a

  • 0

like the title says, I have a single bug in a script for a game I’m making. I have, inside a defined function, an if statement that is supposed to run only if a variable is less than one. But when the function runs, even if this variable is equal to or greater than one, the if statement still runs, and doesn’t go to else. Here’s my source code:

from sys import exit
from sys import argv
from random import shuffle
import random


script = argv


#-------------------------------------------------------------------------

start_room = "your starting point"
top_room = "room with a magical orb"
top_hallway = "long hallway"
mask_room = "room with a golden mask"
fog_hallway = "hallway filled with fog"
fountain_hallway = "hallway with the magical fountain of life at the end"
bottom_room = "room with a magical fire burning in the centre"
bottom_hallway = "round tunnel infested with huge rats"
electric_hallway = "hallway with an electric feel, a crackling in the air"
i = 0
numbers = []
death_toll = 0

#-------------------------------------------------------------------------

def dead():
    print "You died."
    exit(0)

# next few lines to def cheat_death() will be for generating random numbers for use in cheat_death()
while i < 6:
    numbers.append(random.randrange(1,9000))

    i = i + 1

# Now, using the six numbers generated above, I'll turn the string list of numbers[], make it a floating number, and then put them into a question.
# Using this question, if the user answers correctly, they will be taken to start(). If not, they will go to dead().

def cheat_death(numbers):
    shuffle(numbers)

    question = "%d + %d - %d + %d - %d + %d" % tuple(numbers)

    print "You have a single chance to cheat death. To live, please answer the question correctly below:"
    print question

    answer = eval(question)
    print answer

    user_answer = raw_input("> ")

    death_toll == death_toll + 1

    if str(user_answer) == str(answer) and death_toll < 1:
        start()
    else:
        dead()

def end_of_hallway_four():
    print "You come to the end of the hallway.\n\tYou find a big gold coin on the top of a pedestal, sitting on a padded seat."
    print "Around the pedestal there is a wide circle of metal."
    print "Would you like to take the expensive gold coin?"

    take_coin = raw_input("> ")

    if take_coin == "yes" or take_coin == "y" or take_coin == "sure" or take_coin == "yeah":
        cheat_death(numbers)
    elif take_coin == "no" or take_coin == "n":
        print "Very wise indeed."
        exit(0)
    else:
        cheat_death(numbers)

def hallway_four():
    print "You are now in a %s" % electric_hallway
    print "Would you like to see what's at the end of the hallway?"

    end_of_hallway = raw_input("> ")

    if end_of_hallway == "yes" or end_of_hallway == "y" or end_of_hallway == "sure":
        end_of_hallway_four()
    elif end_of_hallway == "no" or end_of_hallway == "n" or end_of_hallway == "nah":
        print "You don't know what you're missing!"
        exit(0)
    else:
        cheat_death(numbers)

def hallway_three():
    print "You are now in a %s" % fountain_hallway
    print "Would you care for a drink of the fountain of life?"

    drink = raw_input("> ")

    if drink == "yes" or drink == "y" or drink == "sure" or drink == "yeah":
        print "You feel very much rejuvinated, and full of energy. You'll live forever!"
        exit(0)
    elif drink == "no" or drink == "n":
        print "Feeling suddenly tired, you quit your journey for a quick nap."
        exit(0)
    else:
        cheat_death(numbers)

def hallway_two():
    print "You are now in a %s" % fog_hallway
    print "Would you like to follow the door at the end of the hallway?"

    hallway_two_door = raw_input("> ")

    if hallway_two_door == "yes" or hallway_two_door == "y" or hallway_two_door == "yeah" or hallway_two_door == "sure":
        hallway_three()
    elif hallway_two_door == "no" or hallway_two_door == "n":
        hallway_one()
    else:
        cheat_death(numbers)

def gold_room():
    print "You are in a %s" % mask_room
    print "Do you take the valuable mask?"

    take_mask = raw_input("> ")

    if take_mask == "yes" or take_mask == "y" or take_mask == "yeah" or take_mask == "sure":
        cheat_death(numbers)
    elif take_mask == "no" or take_mask == "n" or take_mask == "nah":
        print "Wise indeed."
        hallway_one()
    else:
        cheat_deaath()

def rat_hallway():
    print "You are now in a %s" % bottom_hallway
    print "Do you want to follow the light at the end of the tunnel?"

    follow_light = raw_input("> ")

    if follow_light == "yes" or follow_light == "y" or follow_light == "sure" or follow_light == "yeah":
        print "You find yourself at the end of the tunnel, where there is a single door. You follow this door."
        hallway_four()
    elif follow_light == "no" or follow_light == "n":
        exit(0)
    else:
        cheat_death(numbers)

def hallway_one():
    print "You are now in a %s" % top_hallway
    print "At the end of the %s, you find two doors. One looks gold plated, and the other one looks moist and mossy.\nWhich one do you take?" % top_hallway

    top_hallway_door = raw_input("> ")

    if top_hallway_door == "1":
        gold_room()
    elif top_hallway_door == "2":
        hallway_two()
    else:
        cheat_death(numbers)

def fire_room():
    print "You are now in a %s" % bottom_room
    print "Would you like to go through door #1, or door #2?\nYou came from door #2."

    fire_door = raw_input("> ")

    if fire_door == "1":
        rat_hallway()
    elif fire_door == "2":
        start()
    else:
        cheat_death(numbers)

def orb_room():
    print "You are now in a %s" % top_room
    print "There are two doors you could take. Which one do you choose?"

    orb_door = raw_input("> ")

    if orb_door == "1":
        start()
    elif orb_door == "2":
        hallway_one()
    else:
        cheat_death(numbers)

def start_game_question():    
    print """
    You will be asked a series of questions.
    Your answer will either take you to a different room, or perform an action.
    If you come to a point where you are exited from the game, don't quit!
    There's many more rooms to discover!"""
    print "\nTo answer questions, either enter the door number,\nor answer with a word if asked."
    print "\nYour progress will NOT be saved."
    print "To restart the game, just enter python %s." % script
    print "\n\nAre you read to play?"

    start_game = raw_input("> ")

    if start_game == "yes" or start_game == "y" or start_game == "sure" or start_game == "yeah":
        start()
    elif start_game == "no" or start_game == "n" or start_game == "nah":
        exit(0)
    else:
        "Please try again."
        start_game_question()

def start():
    print "You are now at %s." % start_room
    print "Would you like to go through door #1 or door#2?"

    starting_door = raw_input("> ")

    start_door = starting_door

    if start_door == 1:
        orb_room()
    elif start_door == 2:
        fire_room()
    else:
        cheat_death(numbers)

start_game_question()

And here’s the related functions in question:

i = 0
numbers = []
death_toll = 0

def dead():
    print "You died."
    exit(0)

# next few lines to def cheat_death() will be for generating random numbers for use in cheat_death()
while i < 6:
    numbers.append(random.randrange(1,9000))

    i = i + 1

# Now, using the six numbers generated above, I'll turn the string list of numbers[], make it a floating number, and then put them into a question.
# Using this question, if the user answers correctly, they will be taken to start(). If not, they will go to dead().

def cheat_death(numbers):
    shuffle(numbers)

    question = "%d + %d - %d + %d - %d + %d" % tuple(numbers)

    print "You have a single chance to cheat death. To live, please answer the question correctly below:"
    print question

    answer = eval(question)
    print answer

    user_answer = raw_input("> ")

    death_toll == death_toll + 1

    if str(user_answer) == str(answer) and death_toll < 1:
        start()
    else:
        dead()

The specific issue code:

    def cheat_death(numbers):
    shuffle(numbers)

    question = "%d + %d - %d + %d - %d + %d" % tuple(numbers)

    print "You have a single chance to cheat death. To live, please answer the question correctly below:"
    print question

    answer = eval(question)
    print answer

    user_answer = raw_input("> ")

    death_toll == death_toll + 1

    if str(user_answer) == str(answer) and death_toll < 1:
        start()
    else:
        dead()

So I added inside the issue function the global descriptor, and edited a couple other lines, to get this function below:

def cheat_death(numbers):
shuffle(numbers)

    question = "%d + %d - %d + %d - %d + %d" % tuple(numbers)

    print "You have a single chance to cheat death. To live, please answer the question correctly below:"
    print question

    answer = eval(question)
    print answer

    user_answer = raw_input("> ")

    global death_toll

    death_toll = death_toll +1

    if str(user_answer) == str(answer) and death_toll == 1:
        start()
    else:
        dead()

And here’s the terminal output of the problem occuring:

nathan@jolicloud:~/Documents/python$ python gamenew.py

    You will be asked a series of questions.
    Your answer will either take you to a different room, or perform an action.
    If you come to a point where you are exited from the game, don't quit!
    There's many more rooms to discover!

To answer questions, either enter the door number,
or answer with a word if asked.

Your progress will NOT be saved.
To restart the game, just enter python ['gamenew.py'].


Are you read to play?
> yes
You are now at your starting point.
Would you like to go through door #1 or door#2?
> 3
You have a single chance to cheat death. To live, please answer the question correctly below:
4863 + 5960 - 4251 + 3934 - 2638 + 5900
13768
> 13768
You are now at your starting point.
Would you like to go through door #1 or door#2?
> 3
You have a single chance to cheat death. To live, please answer the question correctly below:
3934 + 4251 - 5900 + 4863 - 2638 + 5960
10470
> 10470
You died.
nathan@jolicloud:~/Documents/python$ 

As you can see, the correct way for it to run would be for it to ask for the answer to the question, and then upon getting it right, take the user back to the start, which it does. But then, if you get asked to answer the question again, which you shouldn’t be in the first place, it’ll say that the correct answer is wrong

So you can see, what I want to do is start out with death_toll being 0, meaning that the user has one chance to “die.” Then, if the function cheat_death() gets called, it should increase the variable death_toll by 1, meaning they have no more chances to “die.” Am I doing this right? Because in the end, I want to have the user taken to else if death_toll is equal to, or greater than, 1.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:29:07+00:00Added an answer on May 24, 2026 at 11:29 am

    This statement does not increment death_toll:

    death_toll == death_toll + 1
    

    It is a comparison that returns False. You want:

    death_toll = death_toll +1
    

    Edit:

    If I understand your various comments, etc., what you want is for the program to offer, throughout the entire runtime, one chance to “cheat death”. If the player succeeds on that chance, they return to the start, but after that, if they try to “cheat death” again, they have no chance.

    So, you have multiple problems. As has already been pointed out, you need to use the assignment operator when incrementing the death toll, and you need to use a global declaration to access the global death toll counter within a function. It sounds like the third problem is that the order of statements in cheat_death is simply wrong. If you don’t want the player to have a chance once the death toll has been incremented, then yo need to check it at the beginning of the function.

    I think that I would rewrite the whole function as follows:

    def cheat_death(numbers):
        global death_toll
    
        if death_toll < 1:
    
            # This counts as a used chance.  Increment the counter.
            death_toll = death_toll + 1
    
            shuffle(numbers)
    
            question = "%d + %d - %d + %d - %d + %d" % tuple(numbers)
    
            print "You have a single chance to cheat death. To live, please answer the question correctly below:"
            print question
    
            answer = eval(question)
            print answer
    
            user_answer = raw_input("> ")
    
            if str(user_answer) == str(answer):
                start()
            else:
                dead()
        else:
            dead()
    

    (Well, I’d probably rewrite it further to return something instead of calling start() directly, but I’m not going to try to redesign the whole program.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Like the title says, I have the following exception: Description: Event code: 3005 Event
Like the title says. I find a lot of the issues I have are
Like the title says, how do I redirect every url that starts with http://example.com/shop
As title says, I have a yaml file with values like development: email:dev@abc.com test:
Basically, what the title says. I have several properties that combine together to really
Like the title says Where and How (i.e. if encrypted, with what method) does
Like the title says, how do you create custom code snippets in Visual Studio
Like the title says, If I place an app_offline.htm in the application root, will
Like the title says, the error I get is the following: An invalid '/'
As the title says. I'd like for my application to start when the Windows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.