global name is not defined error - python

My code:
import sys
import time
import random
def main():
print('***TEST**** Grad School Multiplier=',gradschoolmultiplier,'***TEST***')
x=gradschoolmultiplier*50000
print('Your salary in dollars, $',x)
def start():
gradschool=input('Do you intend to go to Graduate School? ')
print('')
time.sleep(2)
if gradschool=='yes':print('That is a fantastic if expensive decision.')
elif gradschool=='Yes':print('That is a fantastic if expensive decision.')
elif gradschool=='Y':print('That is a fantastic if expensive decision.')
elif gradschool=='y':print('That is a fantastic if expensive decision.')
elif gradschool=='YES':print('That is a fantastic if expensive decision.')
else:print('No? Well, then it\'s off to work to pay back those student loans.')
print('')
if gradschool=='yes':g1=3
elif gradschool=='Yes':g1=3
elif gradschool=='Y':g1=3
elif gradschool=='y':g1=3
elif gradschool=='YES':g1=3
else:g1=1
g=random.randrange(1, 3)
if g==1:gradschoolmultiplier=1
else:gradschoolmultiplier=g1*g/2
time.sleep(2)
main()
start()
And of course I get:
NameError: global name 'gradschoolmultiplier' is not defined
I am not smart enough to understand the answers to this question for others. Would someone be so kind as to explain the answer in simpletons' terms? Thanks!

Indeed as #Dan says, the scoping problem.
Or you can use global variables.
Some of my other suggestions on your code:
import sys
import time
import random
def print_salary():
print('***TEST**** Grad School Multiplier=',gradschoolmultiplier,'***TEST***')
x = gradschoolmultiplier*50000
print('Your salary in dollars, $',x)
def main():
gradschool=input('Do you intend to go to Graduate School? ')
print('')
time.sleep(2)
if gradschool.lower() in {'yes', 'y'}:
print('That is a fantastic if expensive decision.')
g1 = 3
else:
print('No? Well, then it\'s off to work to pay back those student loans.')
g1 = 1
print('')
g = random.randrange(1, 3)
global gradschoolmultiplier
if g == 1:
gradschoolmultiplier = 1
else:
gradschoolmultiplier = g1 * g / 2
time.sleep(2)
print_salary()
if __name__ == '__main__':
main()
You should combine some the if statements to make it simpler.
Oh, we share the same thoughts #jonrsharpe
Quick improvement as suggested by #Nils

gradschoolmultiplier is not in the scope of main(), it only exists in start().
You can pass it into main.
change the call to main to be main(gradschoolmultiplier)
change def main() to def main(gradschoolmultiplier):
Information on python scoping

Related

While statement ignoring function

I am very new to using classes and creating functions within it. I have been trying to write a bit more elegant code to the point where the only line to be 'executed' is 1 line, 'o.pag' in this case.
However, my pag function is just not working out. To my eyes, though I've made several variations of the same and even tried to turn it into an series of 'if' statements, gets stuck on "Would you like to play again'?.
Here the problematic but:
def pag(self):
o.game()
o.q()
while p_again == 'yes':
o.game()
o.q(). <<<<<<THIS IS WANT WONT EXECUTE AFTER TYPING 'YES'
if p_again != 'yes':
break
Here is the complete code sample. I am wondering if I caused the problem myself by trying to put everything into functions. Any help would be greatly appreciated.
import random
a = random.randint(1, 9)
#a = 4
again = ''
y = 'yes'
n = 'no'
g= 0
p_again = ''
class olv(object):
def glogic(self):
if g > a:
print('You guessed too HIGH.')
elif g < a:
print('You guessed too LOW.')
else:
print(g)
def game(self):
global g
while g != a:
g = int(input('Guess a number between 1 and 9! '))
o.glogic()
if g == a:
print('Wow, you guessed it.')
def q(self):
global p_again
p_again = str(input('Would you like to play again? '))
def pag(self):
o.game()
o.q()
while p_again == 'yes':
o.game()
o.q()
if p_again != 'yes':
break
o = olv()
o.pag()
you need to put in inside function not out side the function.
code should look like this.
def pag(self):
o.game()
o.q()
while p_again == 'yes':
o.game()
o.q(). <<<<<<THIS IS WANT WONT EXECUTE AFTER TYPING 'YES'
if p_again != 'yes':
break
I believed that this guessing game can run without using classes. From your code, it shows that your basics towards classes is not familiar enough.
You can try to make this guessing game without using classes first. Then you can try to implement the game using classes.
Here is an example of using classes for this game:
import random
p_again = ""
class olv:
def __init__(self):
self.answer = random.randint(1, 9)
self.guess = 0
def game(self):
while self.guess != self.answer:
self.guess = int(input('Guess a number between 1 and 9: '))
if self.guess > self.answer:
print('You guessed too HIGH.')
elif self.guess < self.answer:
print('You guessed too LOW.')
elif self.guess == self.answer:
print("Wow, you guessed it")
else:
print("You should guess between 1 and 9")
while True:
o = olv()
o.game()
p_again = str(input('Would you like to play again? ')).lower()
if p_again != "yes":
break
As pointed out in the comments, you have most of the code logic in there; however there is some confusion about OOP. One great thing to learn is to abandon (at least mostly) global variables, and use classes so that they can keep state of things. Another great suggestion I have is to use meaningful names, for variables and methods.
Now, it is a bit difficult to comment on each line of your code, so I took the liberty to rewrite it a bit. Keep in mind, there are dozen ways to do the same thing, so mine is not the best, nor (hopefully) the worse. I'll add comments to the code:
import random
class Game(object):
# First, let's use meaningful names
def __init__(self):
# Being a class, we can have an init method, and we can use it to initialize
# some variables that we will use to maintain "state". This means, we will give
# the class the job to remember what these values are during the course of the
# program, instead of storing them as global variables
#
# These variables are accessible by ALL methods in the class, using `self.`
self.value_to_guess = -1 # Note: we make value_to_guess and user_input different on purpose
self.user_input = 0 # so that play_game can loop without too much extra code
self.continue_play = True
def game_logic(self):
# This method is in charge of the game logic, which is "compare the values and print something"
# In your original code you did it for only two cases (>, <); it would be a good idea to also
# consolidate the case of equality
if self.user_input > self.value_to_guess:
print('You guessed too HIGH.')
elif self.user_input < self.value_to_guess:
print('You guessed too LOW.')
else:
print('Wow, you guessed it.')
def game_play(self):
# This method is in charge of the game play, making sure that a new random value is
# set at the beginning of a cycle, and that the user is asked every time for a value,
# until the user guesses. Every time, `game_logic` is called so that it will print the
# appropriate message on screen
#
self.value_to_guess = random.randint(1, 9)
while self.value_to_guess != self.user_input:
self.user_input = int(input('Guess a number between 1 and 9! '))
self.game_logic()
def question(self):
# This method will ask the user the question, and return the answered string straight out
return str(input('Would you like to play again? '))
def play(self):
# This method in charge of running games+questions, until the user stops answering 'yes'
self.game_play()
while self.question() == 'yes':
self.game_play()
if __name__ == '__main__':
my_game = Game() # Create a new Game object
my_game.play() # Ask the game object `my_game` to start playing
It's a lot of code, but I hope all the comments will help.
Just what you asked for
The problem manifests like this:
You play the game until you guessed the right number. Note that g == a now.
The asks "Do you want to play again?"
game() is called
But since g == a, game() will immediately quit
So, what's the problem? Well, for a new game, a needs to get a new random value.
Other stuff to make you a better programmer
To me it looks like you're typing in a poor text editor. You use short variable names. If you use an IDE which is made for Python, you'll get code completion and you can have better names that actually express what things are. a could be answer, g could be guess etc. Try JetBrains PyCharm. They have a community edition which is already great.
To me, glogic() is incomplete. The "Wow you guessed it" part is in game(). How about
def glogic(self):
if g > a:
print('You guessed too HIGH.')
elif g < a:
print('You guessed too LOW.')
else:
print('Wow, you guessed it.')
The code depends on global variables. Since you already have a class, all the variables could be included there. Please read about def __init__(self) and member variables like self.answer etc.
In q(), you convert the user input into a string. That's unnecessary, because it is already a string.
In pag(), code is duplicate. The logic to execute o.game() and then o.q() is coded twice. If you set p_again = "yes" initially, you can get rid of that duplication.
It seems n, y and again are unused.
To make user input a bit more robust, you could .lower() the input just in case the user enters some captital letters.
Same for numbers: if the user enters a letter, the code will fail with a ValueError. Use try: ... except ValueError: ... to make the game more robust.
I could rewrite the whole code for you, but I think it's worth doing that yourself.

How to edit variable in function, than pass it to another function?

I'am new to python, and i decided to make a simple quiz game.
The idea is:
Your base health is 100, every wrong answered question is -25 hp.
If hp equals 0 you lose.
And here lies the problem. I don't know how to pass the variable from function to a second function (in this case, every question in quiz is a different function)
The problem is that in every function, hp resets to its basic number (100).
Sorry if i poorly described my problem but im not very fluent in english.
Already tried to make a function that contains ,,hp = 100", making it global etc. Also tried various stuff with ,,return".
hp = 100
def test1():
test = input("yes no")
if test == "yes":
print("this is hp test")
print(hp - 25) # should be 100 - 25 = 75
test2()
if test == "no":
print("ok")
input("dead end")
def test2():
test2 = input("yes no")
if test2 == "yes":
print("this is second hp test")
print(hp - 25) # should be 75 - 25 = 50
if test2 == "no":
print("ok")
input("another dead end")
input("start")
test1()
I am not really sure what your trying to achieve here.
But i would suggest using a class that will allow you to have better control over your variables.
class Game:
def __init__(self):
self.hp = 100
def takeInput(self):
self.current = input()
self.computeScore()
def computeScore(self):
if self.input ==="Something":
self.hp -= 25
if self.checkValidScore():
self.takeInput()
else:
print "game over"
def checkValidScore(self):
return self.hp < 0
The statement print(hp - 25) simply prints the value of hp minus 25. It does not actually modify the value of hp. You probably want:
hp = hp - 25
print(hp)
Use global inside each function declartion
hp = 100
def test1():
global hp # do this in each of your functions
test = input("yes no")
if test == "yes":
print("this is hp test")
hp -= 25 # which is equivalent to hp = hp - 25
print(hp) # here just print the updated variable
test2()
Keep in mind that using global variables is not considered a good practice because it might make your code very hard to debug. You can read more about it here.
Not sure what you want to achieve. If it keeps that simple you could also go in the following direction...
def test1(hp):
test = input("yes no")
stop_msg = None
if test == "yes":
print("not ok")
hp -= 25
elif test == "no":
print("ok")
stop_msg = "dead end"
else:
raise Exception("Expected input to be 'yes' or 'no'.")
return hp, stop_msg
def test2(hp):
test = input("yes no")
stop_msg = None
if test == "yes":
print("'yes' sucks")
hp -= 25
elif test == "no":
print("ok")
stop_msg = "another dead end"
else:
raise Exception("Expected input to be 'yes' or 'no'.")
return hp, stop_msg
def run_game(hp=100):
print("start...")
tests = [test1, test2]
for test in tests:
hp, stop_msg = test(hp)
print("hp: {}".format(hp))
if stop_msg:
print(stop_msg)
return
if __name__ == "__main__":
run_game()
Remarks:
If you want to implement a more complex decision tree, you could use any simple tree representation.
If you have always the same structure within testX functions, introduce one function with parameters for questions, answer, etc.

Python not importing data structure [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm trying to create a text-based game. I've made a basic/generic intro and now it's getting long so I want to create a new file so that I can add things like armor and weapons. When I went to try to import 'my_character' it gave me an error so I went to research this problem and I found this, so I tried it. I just got an error message saying it wasn't define (similar to the error below). This is the intro part named intro:
# Date started: 3/13/2018
# Description: text based adventure game
import time
def display_intro():
print('It is the end of a 100 year war between good and evil that had\n' +
'killed more than 80% of the total human population. \n')
time.sleep(3)
print('The man who will soon be your father was a brave adventurer who \n'
+ 'fought for the good and was made famous for his heroism. \n')
time.sleep(3)
print('One day that brave adventurer meet a beautiful woman who he later \n'
+ 'wed and had you. \n')
time.sleep(3)
def main():
display_intro()
main()
gen = input('\nYour mother had a [Boy or Girl]: ')
name = input("\nAnd they named you: ")
print("You are a {} named {}".format(gen, name))
chara_class = None
# Assigning points Main
my_character = {
'name': name,
'gender': gen,
'class': chara_class,
'strength': 0,
'health': 0,
'wisdom': 0,
'dexterity': 0,
'points': 20
}
# This is a sequence establishes base stats.
def start_stat():
print("\nThis is the most important part of the intro")
time.sleep(3)
print("\nThis decides your future stats and potentially future gameplay.")
time.sleep(4)
print("\nYou have 20 points to put in any of the following category: Strength, Health, Wisdom, or Dexterity.\n"
)
def add_character_points(): # This adds player points in the beginnning
attribute = input("\nWhich attribute do you want to assign it to? ")
if attribute in my_character.keys():
amount = int(input("By how much? "))
if (amount > my_character['points']) or (my_character['points'] <= 0):
print("Not enough points!!! ")
else:
my_character[attribute] += amount
my_character['points'] -= amount
else:
print("That attribute doesn't exist! \nYou might have to type it in all lowercase letters!!!")
def remove_character_points():
attribute = input("\nWhich of the catagories do you want to remove from? ")
if attribute in my_character.keys():
amount = int(input("How many points do you want to remove? "))
if amount > my_character[attribute]:
print("\nYou are taking away too many points!")
else:
my_character[attribute] -= amount
my_character['points'] += amount
else:
print(
"That attribute doesn't exist! \nYou might have to type it in all lowercase letters!!!")
def print_character():
for attribute in my_character.keys():
print("{} : {}".format(attribute, my_character[attribute]))
playContinue = "no"
while playContinue == "no":
Continue = input("Are you sure you want to continue?\n")
if Continue == "yes" or "Yes" or "y":
playContinue = "yes"
start_stat()
add_character_points()
elif Continue == "n" or "No" or "no":
main()
running = True
while running:
print("\nYou have {} points left\n".format(my_character['points']))
print("1. Add points\n2. Remove points. \n3. See current attributes. \n4. Exit\n")
choice = input("Choice: ")
if choice == "1":
add_character_points()
elif choice == "2":
remove_character_points()
elif choice == "3":
print_character()
elif choice == "4":
running = False
else:
pass
def story_str():
print(
"\nYou were always a strong child who easily do physical labor and gain lots of muscle."
)
time.sleep(3)
print("\nYou regularly trained with your dad who was also a skilled swordsman.")
time.sleep(3)
print("\nAs you grew into an adult, you're swordsmanship improved drastically.")
time.sleep(3)
print("\nOnce old enough, you joined the local guild as a warrior.")
time.sleep(3)
def story_dex():
print("\nYou were a sly child. You would always be stealing from other people and with"
+ "\nconstant practice you became proficient at thieving.")
time.sleep(3)
print("\nCombined with the skill of knives and short-blades, you became an extremely deadly assassin."
)
time.sleep(3)
print("\nOnce old enough, you joined the local guild as an assassin.")
time.sleep(3)
def story_wis():
print("\nYou grew up as a very intellegent child. You read books everyday and realized that magic"
+ "is the best offensively and defensively.")
print("\nYou grew up and attended the best magic school avalible and graduated."
)
print("\nYou soon went to the local guild and joined as a wizard.")
run_story = False
while run_story:
if my_character['strength'] >= 13:
story_str()
chara_class = 'Warrior'
run_story = True
else:
continue
if my_character['dexterity'] >= 13:
story_dex()
chara_class = 'Assassin'
run_story = True
else:
continue
if my_character["wisdom"] >= 13:
story_wis()
chara_class = 'Mage'
run_story = True
else:
continue
The command I have typed on part1 is to try to import my_character is:
from intro import my_character
print(my_character)
I have been trying to import my_character but it comes up as:
Traceback (most recent call last):
File "C:/Users/user/Desktop/part1.py", line 5, in <module>
my_character
NameError: name 'my_character' is not defined
The original file was named "intro" and the new one is named "part1". Do I need to do the 'if name == "__main"' thing? If so, what does that do?
Check for:
1) is the file name my_character.py
2) you have imported it as my_character
3) my_character is in the main python directory (if you are importing in interpreter)

The script is not assigning points to the attribute

I'm trying to create a text-based adventure game and all is going well until I encountered a problem with assigning points to attributes. I've been using this website to help with the process but realized that it might be in Python 2. Here's all that I've done so far code:
#Date started: 3/13/2018
#Description: text-based adventure game
import random
import time
def display_intro():
print('It is the end of a 100-year war between good and evil that had \n' +
'killed more than 80% of the total human population. \n')
time.sleep(3)
print('The man who will soon be your father was a brave adventurer who \n' +
'fought for the good and was made famous for his heroism. \n')
time.sleep(3)
print('One day that brave adventurer meet a beautiful woman who he later \n' +
'wed and had you. \n')
time.sleep(3)
def get_gender(gen=None):
while gen == None: # input validation
gen = input('\nYour mother had a [Boy or Girl]: ')
return gen
def get_name(name = None):
while name == None:
name = input("\nAnd they named you: ")
return name
def main():
display_intro()
gender_num = get_gender()
charater_name = get_name()
print("You entered {} {}.".format(gender_num, charater_name))
if __name__ == "__main__":
main()
character_name = get_name()
# Assignning points Main
my_character = {'name': character_name, 'strength': 0, 'wisdom': 0, 'dexterity': 0, 'points': 20}
#This is a sequence establises base stats.
def start_stat():
print("\nThis is the most important part of the intro\n")
time.sleep(3)
print("This decides your future stats and potentially future gameplay.")
time.sleep(4)
print("\nYou have 20 points to put in any of the following category:
Strength, Health, Wisdom, or Dexterity.\n")
def add_charater_points(): # This adds player points in the beginnning
attribute = input("\nWhich attribute do you want to assign it to? ")
if attribute in my_character.keys():
amount = int(input("By how much?"))
if (amount > my_character['points']) or (my_character['points'] <= 0):
print("Not enough points!!! ")
else:
my_character[attribute] += amount
my_character[attribute] -= amount
else:
print("That attribute doesn't exist!!!")
def print_character():
for attribute in my_character.keys():
print("{} : {}".format(attribute, my_character[attribute]))
playContinue = "no"
while playContinue == "no":
Continue = input("Are you sure you want to continue?\n")
if Continue == "yes" or "Yes" or "y":
playContinue = "yes"
start_stat()
add_charater_points()
else:
display_intro()
gender_num = get_gender()
charater_name = get_name()
running = True
while running:
print("\nYou have {} points left\n".format(my_character['points']))
print("1. Add points\n2. Remove points. \n3. See current attributes. \n4. Exit\n")
choice = input("Choice: ")
if choice == "1":
add_charater_points()
elif choice == "2":
pass
elif choice == "3":
print_character()
elif choice == "4":
running = False
else:
pass
And here's what happens when I run it:
It is the end of a 100-year war between good and evil that had
killed more than 80% of the total human population.
The man who will soon be your father was a brave adventurer who fought for
the good and was made famous for his heroism.
One day that brave adventurer meet a beautiful woman who he later wed and
had you.
Your mother had a [Boy or Girl]: boy
And they named you: Name
You entered boy Name.
And they named you: Name
Are you sure you want to continue?
yes
This is the most important part of the intro
This decides your future stats and potentially future gameplay.
You have 20 points to put in any of the following category: Strength,
Health, Wisdom, or Dexterity.
Which attribute do you want to assign it to? strength
By how much? 20
You have 20 points left
1. Add points
2. Remove points.
3. See current attributes.
4. Exit
Choice: 3
name : Name
strength : 0
wisdom : 0
dexterity : 0
points : 20
You have 20 points left
1. Add points
2. Remove points.
3. See current attributes.
4. Exit
Choice:
Oh, and prompt for the name of the play goes again twice for some reason. Also, what does the my_character.keys() under def add_charater_points() mean? Since I just started to learn to code python, if there are any other tips you guys can give me it would be greatly appreciated.
The last two lines of this snippet
if (amount > my_character['points']) or (my_character['points'] <= 0):
print("Not enough points!!! ")
else:
my_character[attribute] += amount
my_character[attribute] -= amount
add the character points to the attribute, and immediately subtract them again. I think you might mean
my_character['points'] -= amount
Your repeated prompt is probably because you have a whole lot of code that logically seems to belong in function main() but is coded to run after main() finishes.

issues importing a script

Hello i have been working on this simple script and i have run into some rather annoying problems that i can not fix myself with the def. and import function it just won't work. here is the main script
import time # This part import the time module
import script2 # This part imports the second script
def main():
print("This program is a calaulater have fun using it")
name = input("What is your name? ")
print("Hello",name)
q1 = input("Would you like to some maths today? ")
if q1 == "yes":
script2 test()
if q1 == "no":
print("That is fine",name,"Hope to see you soon bye")
time.sleep(2)
if __name__ == '__main__':
try:
main()
except Exception as e:
time.sleep(10)
And then the second script is called script2 here is that script as well
import time
def test():
print("You would like to do some maths i hear.")
print("you have some truely wonderfull option please chooice form the list below.")
That is my script currently but it deos not work please help me.
Firstly your indentation doesn't seems to be right. As zvone stated. Secondly you should use script2.test() instead of script2 test(). A functional code is
import time # This part import the time module
import script2 # This part imports the second script
def main():
print("This program is a calaulater have fun using it")
name = input("What is your name? ")
print("Hello",name)
q1 = input("Would you like to some maths today? ")
if q1 == "yes":
script2.test()
if q1 == "no":
print("That is fine",name,"Hope to see you soon bye")
time.sleep(2)
if __name__ == '__main__':
try:
main()
except Exception as e:
time.sleep(10)
This is an error:
def main():
#...
q1 = input("Would you like to some maths today? ")
if q1 == "yes":
# ...
Firstly, the q1 in main() and the q1 outside are not the same variable.
Secondly, if q1 == "yes": is executed before q1 = input(...), because main() was not called yet.
The solution would be to return the q1 value from main and only then use it:
def main():
# ...
return q1
if __name__ == '__main__':
# ...
result_from_main = main()
if result_from_main == "yes":
# ...
Of course, the all names are completely messed up now, but that is a different problem...

Categories

Resources