Monster Racer Rush
Select between 5 monster racers, upgrade your monster skill and win the competition!
4.18 / 5.00 3,534 ViewsBuild and Base
Build most powerful forces, unleash hordes of monster and control your soldiers!
3.80 / 5.00 4,200 ViewsHey NG:
Long story short, I'm trying to write a Python code for an IRL luck game I made a while back. However, it keeps giving me NameErrors based on a set of variables that I've already defined. The relevant bits are as follows (sorry if it's formatted badly, this is my first post using the "code" html):
import random
random.seed()
bank = float(0.00)
def roll_1or2():
cval = random.randint(1,2)
d1val = random.randint(1,6)
d2val = random.randint(1,6)
if cval == 1:
cval = "Heads"
return cval
else:
cval = "Tails"
return cval
return d1val
return d2val
def mainprog():
throwconf = input("Addem once? ('yes' or 'no')")
if throwconf == "yes":
roll_1or2()
print(' ...the coin lands on', cval)
print(' ...the first die lands on', d1val)
print(' ...the second die lands on', d2val)
if cval == "Tails":
bank = bank - 5.00
return bank
games = games + 1
return games
print("loser")
else:
print("winner")
elif throwconf == "no":
home()
else:
throwconf = input("Command invalid. Please retype.")
It's fine until the bit in mainprog when it has to input the cval. It gives me:
Traceback (most recent call last):
File "[filepath]", line 54, in <module>
mainprog()
File "[filepath]", line 15, in mainprog
print(' ...the coin lands on', cval)
NameError: global name 'cval' is not defined
It's treating the code like roll_1or2() isn't even in there. I've tried defining cval globally first as 0, but then it returns a 0 for the cval in mainprog and ignores the return from roll_1or2. And even if it gives me a bad output like that, all it does is move on to d1val and do the same thing over again. Same with d2val if d1val is globally defined. What is this getting tripped up on? I had this working beautifully at one point... it return a H/T value for the coin and a good numerical value for the dice... and then I did something... and now it won't work. Any help?"
Never mind. I decided it would just be easier to globalize the vars in the first place, although I would much prefer to be able to subroutine them. I still wish I knew why it wouldn't return the vars back to global though... oh well. Problem solved.
No, spoke too soon (kind of talking to myself here). Now it's keeping the variables intact, but it's making them a static random number. Does anyone know how to make them a new random number each time they're called?
Just a quick question. Why are you doing this in Python?
= + ^ e * i pi 1 0
I found a few problems with the code snippet your provided. First, you're going to run into problems with returning data from the function "roll_1or2". You are returning two values, but Python is designed to stop after the first return, so the second return will just be dead code and will not do anything.
Second, the reason you're getting errors is because you're attempting to use variables that are local-scope to "rol_1or2" as if they're local to "mainprog()". They aren't. This is where you're having the problem specifically:
print(' ...the coin lands on', cval)
print(' ...the first die lands on', d1val)
print(' ...the second die lands on', d2val)
Third, you're doing the same thing with returns in "mainprog" that you did with "roll_1or2". A function/method ends with its return or, if you didn't provide one, the last line. You have some major reconstruction needed for this portion of the program.
You may want to do something like this:
import random
class Game() :
bank = 0
cval = random.randint(1, 2)
d1val = random.randint(1, 6)
d2val = random.randint(1, 6)
def roll_1or2(self) :
if self.cval == 1 :
self.cval = "Heads"
else :
self.cval = "Tails"
def doChance(self) :
throwconf = input("Addem once? ('yes' or 'no')")
if throwconf == "yes" :
roll_1or2()
print('... the coin lands on', self.cval)
print('... the first die lands on', self.d1val)
print('... the second die lands on', self.d2val)
if self.cval == "Tails" :
bank = bank - 5
games = games + 1
print("loser")
else :
print("winner")
elif throwconf == "no" :
home()
else :
throwconf = input("Command invalid. Please retype.")