【问题标题】:NameError: name 'correct_answer' is not definedNameError:名称“正确答案”未定义
【发布时间】:2022-06-15 17:35:45
【问题描述】:

所以我在玩一个小项目,并一遍又一遍地遇到相同的 NameError。这是有问题的代码:

import time 
import random

print("Here is a practice riddle for you:")
riddler = ["What is nowhere but everywhere, except where something is?", "What is 
green and then red?","What is strong enough to smash ships but still fears the 
sun?","The more you cut me the bigger I grow. What am I?"," A diamond plate, a 
glowing grate, a place you never leave. Where am I?"]
answers = ["Nothing", "Frog in a blender", "Ice", "A hole", "Home"]
riddle = print (random.choice(riddler))

if riddle == [riddler[0]]:
    correct_answer = [answers[0]]
elif riddle == [riddler[1]]:
    correct_answer = [answers[1]]
elif riddle == [riddler[2]]:
    correct_answer = [answers[2]]
elif riddle == [riddler[3]]:
    correct_answer = [answers[3]]
elif riddle == [riddler[4]]:
    correct_answer = [answers[4]]

print (correct_answer)

【问题讨论】:

  • 你使用的是riddle = print(),它返回无。
  • 你将riddle定义为打印函数的返回,它是none。尝试打印谜语你会弄明白的。
  • 如果所有条件都不为真怎么办?
  • 您还可以将它与一个永远不会是的数组进行比较

标签: python


猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-24
  • 1970-01-01
  • 2021-04-15
  • 2019-01-26
  • 2021-10-05
相关资源
最近更新 更多