[英]Python is saying that a variable has not been assigned when it has
import random
import time
name=input("Welcome 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
I get an error that says if score>21: NameError: name 'score' is not defined 我收到一个错误,提示如果分数> 21:NameError:未定义名称“分数”
but I have put score=0 so isn't that defining it? 但是我把score = 0定义了,不是吗?
I am guessing you are inputting the value yes
correctly, the issue may be because of the line - 我猜您输入的
yes
正确的值,问题可能出在线路上-
ready=input("Are you ready to play the game yet?").lower
You are assigning the reference to the lower
function in ready
, instead you should call lower()
and assign the return value in ready
. 您要在
ready
中将引用分配给lower
函数,而应该调用lower()
并在ready
分配返回值。 Example - 范例-
ready=input("Are you ready to play the game yet?").lower()
Also, if you want your code to work, when you do not input ready
as yes
, you should set score=0
, before the if
condition - if ready=="yes" or ready=="y":
另外,如果您希望代码正常工作,则当您不输入
ready
作为yes
,应在if
条件之前将score=0
设置为yes
if ready=="yes" or ready=="y":
The variable score
is declared inside an if
block. 可变
score
在if
块内声明。
Suppose ready
is different from "yes"
and "y"
, then the line score=0
is never reached. 假设
ready
与"yes"
和"y"
,则行score=0
永远不会达到。
So if score>21:
raises an error. 因此,
if score>21:
会引发错误。
You have to set a default value like score = 0
before entering to the first if
condition. 在输入第一个
if
条件之前,您必须设置一个默认值,例如score = 0
。
Moreover, you made a typo: 此外,您输入了错误:
ready=input("Are you ready to play the game yet?").lower
You should call the function using .lower()
. 您应该使用
.lower()
调用该函数。
You need to intend the if score>21:
line and everything that follows so that part of the code is included within the upper if coverage. 您需要使用
if score>21:
行和随后的所有内容,以便部分代码包含在较高的if覆盖范围内。
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")
#same with the rest
On a different note, this script might be flawed because there is a lot of possibility that someone will key in something different. 另外,此脚本可能有缺陷,因为有人输入不同内容的可能性很大。 You may want to include in the line:
您可能要包括在该行中:
ready=input("Are you ready to play the game yet? Type 'Y' for Yes").lower()
some more text about what you want the player to specifically put in. 有关您希望播放器专门放置的内容的更多文字。
However, as mentioned above, the main point is lower()
, this contributes to the error. 但是,如上所述,要点是
lower()
,这会导致错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.