[英]Asking user to provide data using for loop in python
我正在運行以下代碼:
list1 = []
no = int(input("enter your no. of subjects"))
for i in range(no):
score = int(input("enter score for subject no ",i))
list1.append(score)
print(list1)
我編寫這段代碼的目的是詢問用戶他/她想給多少科目的分數,然后為每個科目的分數獲取輸入分數。 但它給了我第 4 行的以下錯誤。
“類型錯誤:raw_input() 需要 1 到 2 個位置參數,但給出了 3 個”。
但我不明白這 3 個參數是什么。 當我刪除第 4 行中的 i 時,它工作正常。 但是我想明智地跟蹤分數主題編號。 你能幫忙嗎?
list1 = []
no = int(input("Enter number of subjects: "))
for i in range(no):
list1.append(input("blah " + str(i)))
print(list1)
你不能寫
input("text", i)
因為然后python將i解釋為傳遞給輸入函數的參數,也只是寫
input("text" + i)
不正確,因為 python 不知道如何將字符串和整數加在一起,如果你從字面意義上考慮它是不可能的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.