import random
import time
name=input("Wecome to the game what is your name")
print(("This is a numbers game"),(name),("you will be playing against the computer."))
print("The idea of the game is to get closer to 21 to the computer without going over 21"),
ready="N"
ready=input("Are you ready to play the game yet?").lower
if ready=="yes" or ready=="y":
score=0
while (score)<21 and (ready == "Y" or ready == "Yes" or ready =="YES" or ready == "yes" or ready =="y"):
player1=random.randint(1,21)
score=(score+player1)
time.sleep(3)
print(("you have scored"),(score))
if score <21:
ready=input("Do you want to add more to your score?")
if score>21: *THE PROBLEMS ON THIS LINE HERE*
print("Sorry Over 21 , The Computer Wins!")
else:
print("ok Well done let us see what the computer can do with their turn")
computerscore=0
while(computerscore)<21 and (computerscore)<(score):
computer=random.randint(1,21)
computerscore=(computerscore+computer)
time.sleep(3)
print(("The computer has scored"),(computerscore))
if (computerscore)<=21 and (computerscore)>(score):
print("Sorry the computer wins")
else:
print("You win well done")
break
我收到的错误是,如果得分> 21: NameError:未定义名称“得分”
但是我把得分= 0所以这不是定义它吗?
答案 0 :(得分:3)
我猜您正在正确输入值yes
,问题可能是因为该行 -
ready=input("Are you ready to play the game yet?").lower
您要在lower
中为ready
功能分配引用,而应拨打lower()
并在ready
中指定返回值。示例 -
ready=input("Are you ready to play the game yet?").lower()
此外,如果您希望代码正常工作,当您未将ready
作为yes
输入时,则应在score=0
条件之前设置if
- {{ 1}}
答案 1 :(得分:1)
变量score
在if
块内声明。
假设ready
与"yes"
和"y"
不同,则永远不会到达score=0
行。
所以if score>21:
会引发错误。
在进入第一个score = 0
条件之前,您必须设置默认值if
。
此外,你写了一个错字:
ready=input("Are you ready to play the game yet?").lower
您应该使用.lower()
调用该函数。
答案 2 :(得分:1)
您需要打算23.2
行以及后面的所有内容,以便部分代码包含在上面的if覆盖范围内。
if score>21:
答案 3 :(得分:0)
另一方面,这个脚本可能存在缺陷,因为有很多人可能会输入不同的东西。您可能希望包含在以下行中:
ready=input("Are you ready to play the game yet? Type 'Y' for Yes").lower()
更多关于你希望玩家明确投入的内容的文字。
但是,如上所述,主要观点是lower()
,这会导致错误。