blackjack - Python; NameError: name 'handsum' is not defined -
i coding basic blackjack game, , challenged myself make many functions possible, code looks this:
funca() funcb() funcc() as opposed to:
funca() print("this") print("that") funcb() here problem in code:
def playerturn(): global handsum global cardone global cardtwo global dealersum global upcard global downcard global bet global newcard global dealernewcard str(handsum) str(cardone) str(cardtwo) str(upcard) str(money) print("you lying at", handsum, "with a", cardone, 'and a', cardtwo + '.', "the dealer has a", upcard, "facing up. have $" + money + '.') int(handsum) int(cardone) str(cardtwo) str(upcard) str(money) sleep(1.3) bet=int(input('how bet? ')) sleep(1.3) money-=bet print("your bet $%s." %bet) sleep(2.5) playerinput=input(str(print('''would to: hit (h) stand (s) double (d) stick (sp) enter letter answer: '''))).upper sleep(1) while true: hit() stick() double() quit() and here's error once code reaches function:
traceback (most recent call last): file "c:\users\owner\desktop\python\games\python 3.x\blackjack\blackjack.py", line 20, in <module> start() file "c:\users\owner\desktop\python\games\python 3.x\blackjack\blackjack.py", line 16, in start intro() file "c:\users\owner\desktop\python\games\python 3.x\blackjack\playerturns.py", line 242, in intro playerturn() file "c:\users\owner\desktop\python\games\python 3.x\blackjack\playerturns.py", line 188, in playerturn str(handsum) nameerror: name 'handsum' not defined i using multiple python files (2) game.
in python, line like
str(handsum) means calling function str argument handsum, , disgarding result. not, @ point in function, have variable called handsum in scope. due global handsum line (note capital s in sum) do have named, different variable in scope. meant refer variable, python case-sensitive.
however, don't know why want call str on variable , throw away return value. not declare handsum (you don't need declare python variables, created on assignment, , can bring them scope global keyword) nor convert handsum (if changed capitalisation) string (you need use return value of str(handsum) that), or indeed, @ all.
also, don't want use globals variables. pass them arguments function, or wrap them object. helps compartmentalise problems in future.
Comments
Post a Comment