簡體   English   中英

如何使用循環來編寫 python 程序,該程序接受用戶輸入“第 1 個人的經驗年數?”、第 2 個人、第 3 個人等等?

[英]How do I write a python program using loops that takes the user input "Years of experience of person 1?", person 2, person 3, and so on?

我最近開始學習 python。 我正在嘗試使用 for 或 while 循環編寫一個程序,該程序應根據他們多年的工作經驗將參加活動的參與者分類為“初學者”、“新手”或“專家”。 它首先詢問用戶參加者的人數,然后詢問第 1、2、3 個人的經驗年限,以此類推,直到總數。

我的代碼是:

n = int(input("Please enter the number of attendees: "))
for i in range(1,n+1,1):
  experience = int(input("Please enter the years of experience of person",i))
  if experience<=2:
    print("Beginner")
  elif 3<=experience<=5:
    print("Novice")
  elif experience>5:
    print("Expert")

變量體驗有一個錯誤: raw_input() 從 1 到 2 個位置 arguments 但給出了 3 個

任何幫助表示贊賞。 謝謝你。

使用字符串格式將人員編號替換為提示符,您不能將其作為input()的第二個參數。

experience = int(input(f"Please enter the years of experience of person {i}: "))

在第 3 行錯誤體驗 = int(input("請輸入人員的工作年限",i)) 輸入 function 將只接受一個參數,但您給出了兩個 arguments 參數1="請輸入個人的工作年限"參數2 =i 要做到這一點,您必須刪除第二個參數,即參數 2,因此正確的輸入 function 將是 experience = int(input("請輸入人員的經驗年限"+str(i)))

n = int(input("Please enter the number of attendees: "))
experience = []
for i in range(0,n):
  exp = int(input(f"Please enter the years of experience of person {i+1}"))
  experience.append(exp)
  if experience[i]<=2:
    print("Beginner")
  elif 3<=experience[i]<=5:
    print("Novice")
  elif experience[i]>5:
    print("Expert")
  

使用打印語句,您可以執行以下操作:

print("hello", 123, "something")

在這種情況下,我們傳遞給它三個 arguments (括號之間用逗號分隔的三個東西)。 第一個參數是“hello”,第二個是 123,以此類推

它打印出來

hello 123 something

但這是打印 function 所做的特殊事情。 input function 不這樣做。 它只需要一個參數。

有幾種方法可以修復它,但我建議的方法是更換

"Please enter years of expertise of person", i+1

f"Please enter years of experience of person {i+1}"

這些稱為 f 字符串,如果您想通過 Google 了解更多關於它們的工作原理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM