拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 将输入变量中的资料存盘在串列中

将输入变量中的资料存盘在串列中

白鹭 - 2022-01-24 1994 0 0

我目前正在学习使用 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)
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *