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 6998491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:25:05+00:00 2026-05-27T20:25:05+00:00

[Python] I need to make the first 3 variables (attack, hitpoints, money) global but

  • 0

[Python] I need to make the first 3 variables (attack, hitpoints, money) global but i dont know how because they are random (random.randint). Could someone tell me what the code should be? Thanks!

Edit: My error is something like “local variable ‘money’ referenced before assignment” later in the script.

Heres a link to the full script: http://dl.dropbox.com/u/55052881/fightprogram.txt

Sorry if it’s really sloppy, i just started learning python a week ago.

while roll == 1:

    attack = random.randint(1, 100)
    hitpoints = random.randint(1, 500)   
    money = random.randint(1, 1000)

    attackstr = str(attack)              
    hitpointsstr = str(hitpoints)
    moneystr = str(money)

    print()
    print('Your attack level is ' + attackstr + '.')
    print('You have ' + hitpointsstr + ' hitpoints.')
    print('Your have ' + moneystr + ' coins.')

    print()
    print('Type 1 to reroll.')
    print('Type 2 to proceed.')
    reroll = input()
    reroll = int(reroll)
    if reroll == 2:
        break
  • 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-27T20:25:05+00:00Added an answer on May 27, 2026 at 8:25 pm

    I think you misunderstand what it means for a variable to be global. I strongly suggest reading through Python’s documentation on scopes and namespaces, but I will do my best to summarize in a way that is relevant to your problem.

    A “global variable” is a variable that is in the global namespace. There is a separate global namespace for each module. Besides modules, classes and functions are the only things that will create new scopes. Any new variables you create will be placed in the innermost scope.

    There is a global keyword, which can be used to reference global variables, but this is only necessary when you are assigning to a global variable, for example:

    x = 5
    def foo():
        x = 10
    def bar():
        global x
        x = 20
    
    >>> x      # global variable x
    5
    >>> foo()  # foo creates a new x in the local scope
    >>> x      # global x is unchanged
    5
    >>> bar()  # bar uses global keyword to reference the global x
    >>> x      # global x is now 20
    20
    

    In your isolated code example, attack, hitpoints, and money are already global variables because they are not in any other scope, and they will be modified in each run of the loop. If your code is actually in a class or a function, then adding the line global attack, hitpoints, money to the top of that scope will result in these variables being set in the global namespace for your module.

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

Sidebar

Related Questions

I am developing a GPL-licensed application in Python and need to know if the
I'm still learning Python, and I'd love to know a way to make the
I have some global variables in a Python script. Some functions in that script
I am still very new to python, but I need to interface with some
I have a small python script to make an installation. I need some information
within the frame of a python application, I need to make calls to several
First thing first, I know there are many questions regarding python and lxml on
A python script need to spawn multiple sub-processes via fork(). All of those child
In Python I need to efficiently and generically test whether an attribute of a
Using Python I need to insert a newline character into a string every 64

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.