[英]How to ask user for loop amount
我正在嘗試創建一個小程序,詢問用戶該班有多少名學生報名,然后用戶輸入金額。 但是之后,我需要運行一個循環來運行用戶指定的學生數量。 到目前為止,這就是我所擁有的。
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)
難道不是在python中預定義了類嗎?
因此,您不能將函數稱為“類”。 也使用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();
您忘了給我們指定您的問題。 但是很明顯,您正在嘗試將由輸入函數返回的str轉換為int類型。
x = int (input ("How many students will be entered? "))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.