简体   繁体   中英

How to ask user for loop amount

I am trying to create a small program that asks the user how many students are enrolled in the class and then the user enters the amount. But after that I need a loop to run for the amount of students specified by the user. Here's what I have so far.

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)

Isn't class predefined in python?

So you cant call a function "class". Also use raw_input.

Convert the input value to a string.

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

Define it as a function like so:

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)

Then just call on it:

doSomething();

You forgot to give us your specify your problem. But its apparent that you are trying to convert str --returned by the input function-- to int type.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM