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

  • Home
  • SEARCH
  • 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 7814077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:04:48+00:00 2026-06-02T05:04:48+00:00

Ok! Basically, I have a variable being declared in one function, and I would

  • 0

Ok! Basically, I have a variable being declared in one function, and I would like to use that variable in another function. I do not want to pass parameters, because I feel like there would be a simpler way to do this. This is my code:

#!/usr/bin/python

#import os
import time
print ("Hello and welcome to Pygame!")
time.sleep(1)
print ("Would you like to load? (\"Y/N\")")

def LON():
    loadOrNew = raw_input()

    if loadOrNew == "N":
        hp = 100
        strhp = str(hp)
        lvl = 1
        strlvl = str(lvl)
        atk = 5
        stratk = str(atk)
        defn = 2
        strdefn = str(defn)
        fout = open("pygame.dat", "w")
        fout.write (strhp) 
        fout.write("\n")
        fout.write(strlvl)
        fout.write("\n")
        fout.write(stratk)
        fout.write("\n")
        fout.write(strdefn)
        fout.close()
        FIRSTPLAY()
        return

    if loadOrNew == "Y":
        fin = open("pygame.dat", "r")
        hpstr = fin.readline()
        lvlstr = fin.readline()
        atkstr = fin.readline()
        defstr = fin.readline()
        hp = int(float(hpstr))
        lvl = int(float(lvlstr))
        atk = int(float(atkstr))
        defn = int(float(defnstr))
        fin.close()
        return

    if loadOrNew != "Y" and loadOrNew != "N":
        print("Im sorry, what?")
        LON()
        return

    return

def SAVE():
    fout = open("pygame.dat", "w")
    fout.write(hp)
    fout.write(lvl)
    fout.write(atk)
    fout.close(defn)
    return


def FIRSTPLAY():
    print("man/woman?")
    gender = raw_input()
    if gender != "man" and gender != "woman":
        print("Not valid gender.")
        FIRSTPLAY()

    print("KING - ")    
    print(" Young " + gender + ", you are herby my knight!")
    time.sleep(1)
    print(" My daughter, princess PYTHON, has been captured!")
    time.sleep(1)
    print(" You are to find her, and relieve this world of her captor!")
    time.sleep(1)
    print(" Some say this evil man's name is GAMEMAKER, but we really don't know.")
    time.sleep(1)
    print(" What do you think it is?")
    captor = raw_input()
    time.sleep(1)
    print(" So you think it is " + captor + "?")
    time.sleep(1)
    print("     Very well, find " + captor + " ASAP!")
    PLAY()
    return

def PLAY():
    print hp
    print lvl
    print atk
    print defn
    greenSlime(hp, lvl, atk, defn)
    return

def greenSlime(php, plvl, patk, pdefn):
    MHP = 10
    MLVL = 1
    MATK = 2
    MDEF = 2
    print "Green Slime - "
    print " HP: 10"
    print " LVL: 1"
    print " ATK: 2"
    print " DEF: 2"
    print "ATK OR DEF?"

LON()

I would like to use hp, lvl, atk, and defn variables that were declared in the LON function, in the PLAY function. I’m sure there is a simpler way then passing parameters.

  • 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-06-02T05:04:50+00:00Added an answer on June 2, 2026 at 5:04 am

    Just declaring a variable inside global will not work. The global keyword is to ask the interpreter not to treat something as a local (overriding a global variable with the same name) when assigning a value to it. What you need to do is define the variable at a scope above the functions which are supposed to share it. Note that you don’t need to declare a variable global in function for read-only access. When the interpreter does not find the variable in local scope it automatically looks for it at outer scope. But in case of assignment statement, it creates a new local variable.

    So what you want to do is :

    hp = None
    lvl = None
    atk = None
    defn = None
    
    def LON():
        global hp, lvl, atk, defn
        # rest of LON
    
    # rest of the functions
    

    But I should warn you that globals are bad programming practice and passing parameters is the right way to do it.

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

Sidebar

Related Questions

Basically I have some variables that I don't want to preinitialize: originalTime = None
Basically I have a small template that looks like: <xsl:template name=templt> <xsl:param name=filter />
basically I have a string being passed to a variable.. say @MyParameter = Flower
I have a String variable (basically an English sentence with an unspecified number of
Basically, I want to have my program retrieve various variables from the hard drive
I basically have 7 select statements that I need to have the results output
I have a MyISAM table that basically contains a log. A cluster of machines
I have a variable being passed to my stored proc and it's a filter
Basically we have sales people that request leads to call. Right now it tried
I basically have exp>=level*10 in an else if expression, where level is a variable

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.