【问题标题】:Repeating user input Python重复用户输入 Python
【发布时间】:2021-09-12 18:56:48
【问题描述】:

我试图根据我的算法询问用户他们知道多少家庭成员的信息,介于 1 到 9 之间。我想知道他们选择的问题的次数,所以如果他们认识 3 个家庭成员,它将重复问题 3 次。在提出问题后,我想将响应存储在每个人特定的变量中,前面的示例将创建 3 个不同的存储变量。这就是我所拥有的:

import pandas as pd

while True:
    try:
        familynumber = int(input("How many family member info do you have? Please select from 1 - 9 "))
for i in familynumber:
    age = int(input("Please enter your Age: "))
    ldlc = int(input("Please enter you LDL-C Level: "))
    totalc = int(input("Please enter your Total-C Level: "))
    clinicalCAD = int(input("Does someone have clinical CAD: "))
    gender = input("Please enter a gender ").lower()
    onsetAge = int(input("Please enter CAD onset age: "))
    tx = int(input("Does someone have Tandon Xanthoma: "))

p1input = pd.DataFrame([age, ldlc, totalc, clinicalCAD, gender, onsetAge, tx])

我只是想不通如何完成这项任务。任何帮助将不胜感激。

【问题讨论】:

  • for i in range(familynumber):
  • 我也会先创建数据框,然后在获取值时或在最后批量插入值,但 进行下一次循环迭代之前。

标签: python pandas input


【解决方案1】:

这里是固定代码

import pandas as pd

familynumber = int(input("How many family member info do you have? Please select from 1 - 9 "))

# Define the dataframe
p1input = pd.DataFrame(columns=["age", "ldlc", "totalc", "clinicalCAD", "gender", "onsetAge", "tx"])

# Use range to go in a loop
for i in range(familynumber):
    age = int(input("Please enter your Age: "))
    ldlc = int(input("Please enter you LDL-C Level: "))
    totalc = int(input("Please enter your Total-C Level: "))
    clinicalCAD = int(input("Does someone have clinical CAD: "))
    gender = input("Please enter a gender ").lower()
    onsetAge = int(input("Please enter CAD onset age: "))
    tx = int(input("Does someone have Tandon Xanthoma: "))

    # Add row by row to the dataframe
    p1input.loc[i] = [age, ldlc, totalc, clinicalCAD, gender, onsetAge, tx]

# print head to verify
print(p1input.head())

【讨论】:

  • tx 行将失败 - 您不能在 Y/N 答案上调用 int()
【解决方案2】:

您可以使用命名元组来创建一个代表家庭成员的简单对象。然后,您可以将循环中的每个人员对象存储到人员列表中。

示例

from collections import namedtuple

# Define a Person namedtuple object to store data
Person = namedtuple("Person", "age ldlc totalc clinicalCAD gender onsetAge tx")

# Iterate over family members and generate Person objects
persons = []
familynumber = int(input("How many family member info do you have? Please select from 1 - 9: "))
for i in range(familynumber):
    print(f"{'-'*20}\nQuestions for person #{i+1}:\n{'-'*20}")
    p = Person(
        age = int(input("Please enter your Age: ")),
        ldlc = int(input("Please enter you LDL-C Level: ")),
        totalc = int(input("Please enter your Total-C Level: ")),
        clinicalCAD = input("Does someone have clinical CAD: "),
        gender = input("Please enter a gender ").lower(),
        onsetAge = int(input("Please enter CAD onset age: ")),
        tx = input("Does someone have Tandon Xanthoma: ")
    )

    # Store person in persons list
    persons.append(p)

# Each person stored in the "persons" list can now be accessed
print(f"{persons = }")
print(f"{persons[0].age = }")

【讨论】:

  • 我还修复了 tx 输入,因为您将其转换为整数,即使它在技术上是一个字符串是/否问题。
猜你喜欢
  • 2013-08-24
  • 1970-01-01
  • 2018-05-06
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
  • 2021-07-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多