简体   繁体   中英

Python variable inside of function not able to be called

I am making an rpg type thing with python and I made a few functions but I need to call variables that were stated and altered inside of a separate function. How would I be able to call a variable inside a function. My Code:

import time
def title():
  print("Hello Welcome To Elvoria")
  time.sleep(1)
  print("Choose Your Gender, Name, And Race")
  time.sleep(1)
def gnd():
  Gender = input("""Male, Female, Or NonBinary?
""")

  if Gender == ("Male"):
    genderin = 'Male'
  elif Gender == ("Female"):
    genderin = 'Male'
  elif Gender == ("NonBinary"):
    genderin = 'Male'
  else:
    gnd()
def nm():
  namein = input("""What Is Your Character's Name?
""")
  print("Welcome To Elvoria "+namein+"")
def rac():
  print("""The Race Options Are:
Elf-Good With Archery And Light Armor
Kelner-Good With Two Handed Weapons And Heavy Armor
Human-Good With Bows And Stealth
Mageine-Good With Magic And Speed""")
  time.sleep(1)
  racin = input("""Now Choose A Race
""")
  if racin == ("Elf"):
    print ("You Are Now A " + genderin + racin +" Named" + namein + "")
  elif racin == ("Kelner"):
    print ("You Are Now A " + genderin + racin +" Named" + namein + "")
  elif racin == ("Human"):
    print ("You Are Now A " + genderin + racin +" Named" + namein + "")
  elif racin == ("Magein"):
    print ("You Are Now A " + genderin + racin +" Named" + namein + "")
  else:
    print ("You Are Now A " + genderin + racin +" Named" + namein + "")
title()
time.sleep(1)
gnd()
time.sleep(1)
nm()
time.sleep(1)
rac()

And The Error Code:

   print ("You Are Now A " + genderin + racin +" Named" + namein + "")
NameError: name 'genderin' is not defined

You have 2 options:

  • You could create a global variable, and define that using the global statement in your function, so you can use the global value afterwards

  • You could pass a value to the function, and work with it (the better way, using global variables should be avoided)

Change your functions to return the result, and take the values they need as parameters:

def gnd() -> str:
    gender = input("""Male, Female, Or NonBinary?
""")
    if gender in ("Male", "Female", "NonBinary"):
        return gender
    return gnd()

def nm() -> str:
    name = input("""What Is Your Character's Name?
""")
    print(f"Welcome To Elvoria {name}")
    return name

def rac(gender: str, name: str) -> str:
  print("""The Race Options Are:
Elf-Good With Archery And Light Armor
Kelner-Good With Two Handed Weapons And Heavy Armor
Human-Good With Bows And Stealth
Mageine-Good With Magic And Speed""")
    time.sleep(1)
    race = input("""Now Choose A Race
""")
    print (f"You Are Now A {gender} {race} Named {name}")
    return race

When you call them, make sure to assign the values to variables so you can use them again:

title()
time.sleep(1)
gender = gnd()
time.sleep(1)
name = nm()
time.sleep(1)
race = rac(gender, name)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM