Python-NameError:name';输入';没有定义

Python-NameError:name';输入';没有定义,python,input,nameerror,Python,Input,Nameerror,我试图在Python上构造一个程序。这是问题所在的程序的一部分。当我在Python中运行它时,它通过“task_name=input”行(“任务是什么”)运行,但当我通过Python visualizer运行它时,它给了我以下错误:NameError:name“input”未定义。下面这段程序只是我程序的开始,在它运行完这段程序后,我需要它运行我程序的其余部分,但它会停止并重复这段程序。如果我没有很好地解释这一点,我很抱歉,如果你不明白,尽管问。请回答,我需要在明天之前完成这个程序! 我使用过p

我试图在Python上构造一个程序。这是问题所在的程序的一部分。当我在Python中运行它时,它通过“task_name=input”行(“任务是什么”)运行,但当我通过Python visualizer运行它时,它给了我以下错误:NameError:name“input”未定义。下面这段程序只是我程序的开始,在它运行完这段程序后,我需要它运行我程序的其余部分,但它会停止并重复这段程序。如果我没有很好地解释这一点,我很抱歉,如果你不明白,尽管问。请回答,我需要在明天之前完成这个程序! 我使用过python可视化工具,现在使用的是python 3.2.1.1

newTask = "y"
while newTask == "y":
    try:
        numStudents = int(input("How many students are there?"))
    except ValueError:
             print("An integer please")
             continue
    else:
             if numStudents <=1:
                  print("You must enter more than 1 student.")
    task_name = input("What is the task?")
while numStudents == 0:
  for i in range (1,numStudents):
    cont = "y"
    while cont == "y":
        import random
        vol_num = random.randint (1,numStudents)
        print(vol_num) #remove, needs to be hidden
        break

        print("The task is", task_name, ".", "If your number matches the secret number you must complete this task. Good luck!!")
        print("There are", numStudents-student_num, "numbers left which means you have 1 chance in", numStudents, "of being the volunteer!")

        student_name = input("What is your name? Please enter your first name in letters with a maximum of 15.")
        while student_name.isalpha() != True or len(task_name) > 15:
            print("Your first name must be entered in letters with a maximum of 15 letters.")
            student_name = input("What is your name?")

        student_num == 0
        while student_num == 0:
            try:
                student_num = int(input("What is your number? Please enter it as an integer."))
            except ValueError:
                print("You number must be entered as an integer.")
                continue
            else:
                if student_num < 1 or student_num > numStudents:
                    print("That is not a valid number. Please enter the number you were given.")
                    student_num = int(input("What is your number?"))

        if student_num == vol_num:
                print("You are the volunteer! Please complete the task of", task_name, ".")
        else:
                    print("You were lucky this time! You do not have to complete the task!")

        cont = input("Are you the next student? y/n")
        while cont == "n":
            newTask = input("Would you like to enter a new task? (y/n")
newTask=“y”
而newTask==“y”:
尝试:
numStudents=int(输入(“有多少学生?”)
除值错误外:
打印(“请输入整数”)
持续
其他:
如果Numstudens 15:
打印(“您的名字必须以字母形式输入,最多15个字母。”)
学生姓名=输入(“你叫什么名字?”)
学生人数==0
当student_num==0时:
尝试:
student_num=int(输入(“您的数字是多少?请以整数形式输入”)
除值错误外:
打印(“您的数字必须输入为整数。”)
持续
其他:
如果学生人数<1或学生人数>学生人数:
打印(“这不是一个有效的数字。请输入您收到的数字。”)
student_num=int(输入(“您的号码是多少?”)
如果学生人数=学生人数:
打印(“您是志愿者!请完成”任务名称“.”的任务)
其他:
打印(“这次你很幸运!你不必完成任务!”)
cont=输入(“你是下一个学生吗?是/否”)
而cont==“n”:
newTask=input(“是否要输入新任务?(y/n))

您遇到的错误是因为没有允许用户输入数据的机制

您需要这样做,以便按照自己的意愿以交互方式使用它

附加的

第一,虽然:

while newTask == "y":
正在生成一个无限循环,因为它没有在循环中的任何位置设置
newTask
task\u name
可能是您打算使用
newTask
的地方

或者,我不确定循环是否适合它的用途-下面的代码可能更合适:

numStudents = 0
while numStudents < 2:
    try:
        numStudents = int(input("How many students are there?"))
    except ValueError:
             print("An integer please")
             continue
    else:
             if numStudents <=1:
                  print("You must enter more than 1 student.")
# The line below doesn't make much sense in this code block so I've commented it out
#    task_name = input("What is the task?")
numStudents=0
当numstudent<2时:
尝试:
numStudents=int(输入(“有多少学生?”)
除值错误外:
打印(“请输入整数”)
持续
其他:

如果numStudentswhile循环当前将永远运行,因为大小写始终为true,newTask==“y”。为了退出循环,需要将newTask设置为“y”以外的值,从而导致大小写变为false,newTask=“n”

另一方面,您的程序似乎根本不需要位于newTask while循环中,因为您将数据存储为单个数字和字符串。因此,您可以像这样重写程序:

    numStudents = 0 

    while numStudents <= 1:
        try:
            numStudents = int(input("How many students are there?"))
        except ValueError:
            print("An integer please")
        else:
            if numStudents <= 1:
                print("You must enter more than 1 student.")

    task_name = input("What is the task?")
numStudents=0

虽然numStudents我不会重写你的整个程序,但我会给你一些建议,应该会有所帮助

另一方面,你为什么试图破坏你的帖子?一旦答案被发布,它就不能被删除(除非在某些情况下由社区成员删除,即使这样,高代表性用户仍然可以看到)。此处发布的所有内容都在下获得许可,列在每页底部。基本上,只要您发布内容,就如同编辑维基百科文章一样——它永远在那里,即使有人编辑它。基本上,将其完全删除需要互联网上相当于上帝的行为(堆栈交换系统管理员)

无论如何,如果您还没有(当前版本的Python3)并将其安装到您的计算机上。如果由于某种原因您不能这样做(公共/共享工作站等),然后使用一个免费的云站点,比如-一个帐户只需要一个电子邮件地址。如果你在自己的计算机上工作,我强烈建议你安装一个编程编辑器,比如。它包括很多很酷的功能,比如语法高亮显示、自动括号匹配等*

*如果您想稍微解决一下,请安装,然后安装以下插件:,和。我编写了最后2个插件来帮助Python编程。它们不是必需的,但对我很有帮助。YMMV。只是一个注释-可能会将所有这些东西保存到您的任务完成后。很容易被闪亮的新东西分散注意力:)

好,继续讲节目本身。在Sublime或Pythonywhere中加载它,这两种语言都有行号(如果您正在编程,这是一个非常重要的特性)。我将使用你问题中的程序行号

第一件事:注意你的缩进。Sublime有一个很好的功能,您可以用垂直线显示缩进块的位置(如果安装Neon,它们是深灰色的,不显眼,但仍然可见)。默认情况下,它们应处于启用状态。请记住,缩进在Python中非常重要,变量缩进可能会导致错误。每个缩进块应该有4个空格深,不多也不少,强烈建议不要使用制表符。第6行、第7行、第9行和第10行缩进过度(与上面的行相比,第6行、第7行和第9行缩进9个空格,而不是第4行;第10行缩进5个空格,而不是第4行)。在Sublime中,您可以高亮显示某些文本,然后使用Ctrl[向左移动,或Ctrl]向右移动(使用⌘ 而不是在OS X上按Ctrl键)。第二个
循环中也有更多缩进问题

二线星
while numStudents == 0:
  for i in range (1,numStudents):
list(range(1, 0))
[]
range(stop)
range(start, stop[, step])
>>> list(range(10, 1))
[]
>>> list(range(10, 1, -1))
[10, 9, 8, 7, 6, 5, 4, 3, 2]