我目前正在學習使用 Python 編程的主題,并且即將參加考試。170行的代碼已經上傳了,我的口語考試的安排,但我想改進一些部分。
在第一部分中,我很難在串列中存盤輸入,即使用我的程式來自“受訪者”的身高、體重和出生年份。這些輸入稍后將用于計算 BMI 并提供一些健康提示。
我曾嘗試在許多網站上尋求幫助并使用這些建議。也許我的代碼有問題?
# Input from respondent
Name = input('Type in your name : ')
Birthyear = input('Type in your birth year : ')
Height = input('Type in your height in inches: ')
Weight = input('Type in your weight in pounds : ')
Informations = (f'\n\nName\t\t: {Name.capitalize()}\n'
f'Birthyear\t: {Birthyear.capitalize()}\n'
f'Height\t\t: {Height.capitalize()}\n'
f'Weight\t\t: {Weight.capitalize()}\n')
print(Informations)
在此之后,我希望將輸入存盤在包含關于身高、體重和出生年份的虛構資訊的串列中,如下所示:
# Create list with heights(BMItest_H), weights(BMItest_W) and birth year(BMItest_B) from
fictional BMI test persons
BMItest_H = [70, 80, 78, 78, 75, 74, 77, 76]
BMItest_V = [176, 204, 199, 200, 187, 181, 180, 182]
BMItest_B = [1994, 1992, 1992, 1990, 1989, 1991, 1988, 1990]
uj5u.com熱心網友回復:
您可以簡單地回圈并附加到串列中以獲得多個“受訪者”。
因此,以 10 個受訪者為例:
Birthyear_list = []
Height_list = []
Weight_list = []
name_list = []
respondents = 10
for n in range(respondents):
# Input from respondent
Name = input('Type in your name : ')
Birthyear = input('Type in your birth year : ')
Height = input('Type in your height in inches: ')
Weight = input('Type in your weight in pounds : ')
Informations = (f'\n\nName\t\t: {Name.capitalize()}\n'
f'Birthyear\t: {Birthyear.capitalize()}\n'
f'Height\t\t: {Height.capitalize()}\n'
f'Weight\t\t: {Weight.capitalize()}\n')
Birthyear_list.append(Birthyear)
Height_list.append(Height)
Weight_list.append(Weight)
name_list.append(Name)
print(Informations)
print(Birthyear_list)
print(Height_list)
print(Weight_list)
print(name_list)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/402488.html
標籤:
上一篇:過濾器(Filter)
