我正在编写一个基本的21点游戏,我挑战自己尽可能多地编写函数,代码如下所示:
funcA()
funcB()
funcC()
与之相反的是:
^{pr2}$以下是我的代码中存在的问题:
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 are lying at", handSum, "with a", cardOne, 'and a', cardTwo
+ '.', "The Dealer has a", upCard, "facing up. You have $" + money + '.')
int(handsum)
int(cardOne)
str(cardTwo)
str(upCard)
str(money)
sleep(1.3)
bet=int(input('How much would you like to bet? '))
sleep(1.3)
money-=bet
print("Your bet is $%s." %bet)
sleep(2.5)
playerInput=input(str(print('''Would you like to:
Hit (H)
Stand (S)
Double (D)
Stick (SP)
Enter Letter for Answer: '''))).upper
sleep(1)
while True:
Hit()
Stick()
Double()
Quit()
下面是我的代码到达此函数时遇到的错误:
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' is not defined
我在这个游戏中使用了多个python文件(2)。在
在python中,一行
意味着您用参数
handsum
调用函数str
,然后丢弃结果。此时,在函数中,范围内没有名为handsum
的变量。由于你的global handSum
行(注意大写字母S),你的确实有一个名称相似但范围不同的变量。您可能想引用这个变量,但是python是区分大小写的。在但是,我不知道为什么要对一个变量调用
str
并丢弃返回值。这不会声明handSum
(您不需要声明python变量,它们是在赋值时创建的,您可以使用global
关键字将它们带到作用域中),也不会将handSum
(如果您更改了大写字母)转换为字符串(您将需要为此使用返回值str(handSum)
),或者实际上,执行任何操作。在另外,您不希望对所有这些变量使用
global
s。将它们作为参数传递给函数,或将它们包装到对象中。这有助于区分未来的问题。在相关问题 更多 >
编程相关推荐