简体   繁体   English

Python说的是,变量没有赋值时

[英]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. 可变scoreif块内声明。

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.

相关问题 如何检查 Python 是否已分配变量 - How to check in Python if a variable has been assigned or not Python在安装的时候说一个模块没有安装 - Python is saying that a module has not been installed when it is installed Python-如何打印已分配给缩进的变量? - Python - How to print variable that has been assigned to an indentation? 如何获取已分配给小部件的图像变量? - How to get the image variable that has been assigned to a widget? Python说在赋值之前提到的变量在嵌套在for循环中时被赋值吗? - Python saying variable mentioned before assignment when it is assigned in the for loop it's nested in? 有没有办法让文本仅在分配对话框中的变量后才显示? - Is there a way for a text to only show once a variable from a dialog box has been assigned? 我怎样才能得到一个函数来存储一个新变量,并在它被传递后分配一个值 - how can I get a function to store a new variable, with a value assigned to it, after it has been passed 为什么Python说pow只有2个参数 - Why is Python saying pow only has 2 arguments 使用ndb和python从通过GAE中的URL传递的自动分配的ID中检索密钥 - Retrieve key from automatically assigned ID which has been passed through URL in GAE, using ndb and python 在 Python 2.7 中分配 styleObj 后,如何使用 sheet[&#39;A&#39;].style/styleObj 分配样式? - How to assign a style using sheet['A'].style/styleObj after styleObj has been assigned in Python 2.7?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM