gpt4 book ai didi

Python 一直说 NameError : name 'name' is not defined

转载 作者:行者123 更新时间:2023-11-28 22:52:00 27 4
gpt4 key购买 nike

def main():
name = raw_input("What is your name?")
age = raw_input("How old are you?")
color = raw_input("What is your favorite color?")

print "Ah, so your name is %s, your quest is %s, " \
"and your favorite color is %s." % (name, age, color)
if __name__ == '__main__':
main()

我正在尝试从 Codeacademy 复制以下代码:

name = raw_input("What is your name?")
quest = raw_input("What is your quest?")
color = raw_input("What is your favorite color?")

print "Ah, so your name is %s, your quest is %s, " \
"and your favorite color is %s." % (name, quest, color)

最佳答案

打印应该在主函数的范围内,以便访问它的变量:

def main():
name = raw_input("What is your name?")
age = raw_input("How old are you?")
color = raw_input("What is your favorite color?")

print "Ah, so your name is %s, your quest is %s, " \
"and your favorite color is %s." % (name, age, color)

if __name__ == '__main__':
main()

这应该可以。

关于Python 一直说 NameError : name 'name' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20924298/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com