【问题标题】:Storing data from input-variable in a list将输入变量中的数据存储在列表中
【发布时间】:2022-01-03 18:59:54
【问题描述】:

我目前正在学习一门使用 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]

【问题讨论】:

    标签: python python-3.x python-2.7


    【解决方案1】:

    您可以简单地循环并附加到一个列表以获取多个“响应者”。

    以 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)
    

    【讨论】:

    • 嗨 Eli 非常感谢您帮助我。这似乎是一个很好的解决方案。我会继续努力的!享受你的一天!
    • 很高兴它有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-15
    • 2023-03-20
    相关资源
    最近更新 更多