如何询问用户的循环量

时间:2014-09-15 01:45:57

标签: python loops printing

我正在尝试创建一个小程序,询问用户有多少学生在课程中注册,然后用户输入金额。但之后我需要一个循环来运行用户指定的学生数量。这是我到目前为止所拥有的。

def class ():
    x = input ("How many students will be entered? ")
    for count in range (x):
      name = input ("What is your name? ")
      major = input ("What is your major? ")
      year = input ("and your year? ")
      print (name, major, year)

2 个答案:

答案 0 :(得分:1)

是不是在python中预定义了类?

所以你不能打电话给一个功能" class"。也可以使用raw_input。

将输入值转换为字符串。

 x = int (raw_input ("How many students will be entered? "))

将其定义为如下函数:

def doSomething():
    x = int (raw_input ("How many students will be entered? "))
    for count in range (x):
      name = raw_input ("What is your name? ")
      major = raw_input ("What is your major? ")
      year = raw_input ("and your year? ")
      print (name, major, year)

然后请致电:

doSomething();

答案 1 :(得分:0)

您忘了告诉我们您的问题。但很明显,你试图将输入函数转换为str - 转换为int类型。

x = int (input ("How many students will be entered? "))