【问题标题】:Store input in a 2-dimensional list - Python 3x - BMI program将输入存储在二维列表中 - Python 3x - BMI 程序
【发布时间】:2021-08-21 16:10:31
【问题描述】:

我是 Python 新手,仅供参考。我将输入作为患者姓名、患者 ID 号、身高和体重,我有用于计算他们的体重指数的功能代码。用户可以继续存储输入,直到他们“退出”或为患者 ID 输入负值。

问题:我还需要将这些输入存储在二维列表(列表列表)中,然后能够输出所有患者姓名的报告,后跟他们的 BMI 编号,加上状态我已经计算过的类别(肥胖、体重不足等)。以下是我目前所有可用的代码(虽然不漂亮):

# Give the user input options.
print("\nWelcome to patient entry. What would you like to do?")

# Set an initial value for choice other than the value for 'quit'.
choice = ''

# Start a loop that runs until the user enters the value for 'quit'.
while choice != 'q':
    # Give all the choices in a series of print statements.
    print("\n[1] Enter Patient Info.")
    print("[q] Enter q to quit.")

# Ask for the user's choice.
    choice = input("\nWhat would you like to do? ")

# Respond to the user's choice.
    if choice == '1':
        patient_name = (input("Enter patient name: "))
        patient_id = int(input("Enter patient ID number: "))

        while True:
            height = int(input("Enter your height in inches: "))
            try:
                val = int(height)
                if val < 0:  # if not a positive, ask for input again
                    print("Sorry, input has to be a positive number, try again")

                break
            except ValueError:
                print("That's not an number!")

        while True:
            weight = int(input("Enter your weight in pounds: "))

            try:
                val = int(weight)
                if val < 0:  # if not a positive int, ask for input again
                    print("Sorry, input has to be a positive number, try again")
                    continue
                break
            except ValueError:
                print("That's not an number!")

        # CDC formulat--https://www.cdc.gov/nccdphp/dnpao/growthcharts/training/bmiage/page5_2.html
        BMI = ((float(weight) / float(height) ** 2) * 703)

        # print(f"Your BMI is {BMI}")

        print("Your BMI is: {0} and you are: ".format(round(BMI, 2)), end='')

        # conditions
        if (BMI < 16):
            print("severely underweight")

        elif (BMI >= 16 and BMI < 18.5):
            print("underweight")

        elif (BMI >= 18.5 and BMI < 25):
            print("Healthy")

        elif (BMI >= 25 and BMI < 30):
            print("overweight")

        elif (BMI >= 30):
            print("severely overweight")

    elif choice == 'q':
        print("\nThanks for playing. See you later.\n")
        exit()
    else:
        print("\nI don't understand that choice, please try again.\n")

patient_name = (input("Enter patient name: "))
patient_id = int(input("Enter patient ID number: "))

while True:
  height = int(input("Enter your height in inches: "))
  try:
        val = int(height)
        if val < 0:  # if not a positive, ask for input again
            print("Sorry, input has to be a positive number, try again")
            continue
        break
  except ValueError:
    print("That's not an number!")

while True:
  weight = int(input("Enter your weight in pounds: "))

  try:
        val = int(weight)
        if val < 0:  # if not a positive int, ask for input again
            print("Sorry, input has to be a positive number, try again")
            continue
        break
  except ValueError:
        print("That's not an number!")

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    首先,您需要创建一个列表,您可以在其中添加包含患者数据的其他列表。

    myList = []
    

    现在,一旦您在循环的 1 次迭代中为单个患者获取了所有必需的输入,就可以创建一个新列表(子数组)了。我还注意到您也需要类别,但在计算之后,您只是打印它。所以,创建一个变量并存储它。

    subList = [patient_name, patient_id, height, weight] #add remaining values as well
    

    最后,将subList 添加到myList 以创建一个二维列表,如下所示:

    myList.append(subList)
    

    要遍历此二维列表,您可以执行以下操作:

    for patientData in myList:
        print(patientData)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-20
      • 1970-01-01
      • 2013-08-30
      • 1970-01-01
      相关资源
      最近更新 更多