为什么python在定义时返回“NameError:name 'correct' is not defined?

JustANoobProgrammer

本质上我是在做一个测验,python 返回一个错误,说下面显示的变量在定义后是未定义的。

def guesscheck(guess, answer):
    correct = "null"
    print(lives * '\u2665')
    if guess.upper() == answer:
        print("Marvelous! That is correct!")
        correct = True
        return correct 
    if guess.upper() != answer:
        print("That is incorrect.")
        correct = False
        return correct

lives = 3
guess = input("ok: ")
guesscheck(guess, "OK")
if correct == False:
    lives = lives - 1
    print(lives * '\u2665')

如您所见,我调用了应该将变量“正确”定义为 True 或 False 的函数,并将其返回给程序的其余部分,但由于某种原因,它显示为未定义。请帮我!

恩佐

您应该correct从函数返回变量,然后在调用时访问它guesscheck

def guesscheck(guess, answer):
    # This variable declaration is not necessary
    # correct = "null"
    #
    # Instead, you can assign it to the answer check
    correct = guess.upper() == answer

    print(lives * '\u2665')

    if correct:
        print("Marvelous! That is correct!")

    # You can use an else-clause here
    else:
        print("That is incorrect.")

    return correct

然后像这样访问它:

lives = 3
guess = input("ok: ")
correct = guesscheck(guess, "OK")
# No need for `correct == False`
if not correct:
    lives = lives - 1
    print(lives * '\u2665')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我会收到“NameError: name 'self' is not defined”?

为什么会出现 NameError 的错误:name 'file' is not defined in python 3

为什么此重新加载失败并显示“NameError: name <xxx> is not defined”?

在类中导入模块时出现“NameError: name [module] is not defined”

“NameError: name 'update' is not defined” 使用数据表时出错

如何正确配置python 3的使用避免NameError: name '' is not defined

导入 kafka 给出“NameError: name 'true' is not defined”

NameError: ("name 'true' is not defined", 'occurred at index 0')

如何修复“NameError: name 'context' is not defined”

如何修复“NameError: name 'color' is not defined”?

什么是[if(!defined('ABSPATH'))]

NameError: name '' is not defined - 将函数返回作为输入传递给另一个函数

iPython调试器引发`NameError:name ... not defined`

不明白这个“NameError: name 'self' is not defined”错误

如何解决错误 NameError: name 'SparkConf' is not defined in pycharm

Scrapy:在scrapy 中使用SQLAlchemy 有“NameError: name 'connection' is not defined”

我有一个错误:NameError: name 'GameDisplay' is not defined

错误“NameError: name 'self' is not defined”即使我声明了“self”

无法在 18.04 中添加 PPA 密钥:NameError: name 'KUrl' is not defined

试图从父类继承变量“NameError: name 'r' is not defined”

Django连接mysql问题,NameError: name '_mysql' is not defined

在 leetcode 中,为什么我收到错误“NameError: global name 'climbStairs' is not defined”?但在没有“自我”和“对象”的情况下工作?

如何在 python 中关于海面温度的图中修复“NameError: name 'cartopy' is not defined'?

当我在请求输入时尝试中断 while 循环时,如何克服“NameError: name 'x' is not defined”?

const_defined?与定义?在Ruby中

“ Qed”和“ Defined”有什么区别?

为什么if(DEFINED <variable>)在cmake中不起作用?

为什么会出现 Uncaught ReferenceError: $ is not defined coffeescript in rails 5.2?

为什么会出现错误 Route [dataProcessing] not defined?

TOP 榜单

热门标签

归档