Как я могу выполнить цикл кода с начала вычисления (ввод высоты и веса)? Я пытался использовать def main, но, возможно, использовал его неправильно

#python #python-3.x

Вопрос:

 A="Welcome"
B="Done by: "Name"
C="Class: "Class"
D="IT Final project - CP2"
E="A program that calculates the BMI and describes it as underweight, healthy, overweight, severly overweight, obese, severly obese"
print(A)
print(B)
print(C)
print(D)
print(E)
Height=input("Enter height in meters")
Height=float(Height)
Weight=input("Enter weight in kg")
Weight=float(Weight)
BMI=(Weight/Height**2)
if BMI <= 18.4:
    print(BMI,"You are underweight")
elif BMI <= 24.9:
    print(BMI,"You are healthy")
elif BMI <= 29.9:
    print(BMI,"You are overweight")
elif BMI <= 34.9:
    print(BMI,"You are severly overweight")
elif BMI <= 39.9:
    print(BMI,"You are obese")
elif BMI > 39.9:
    print(BMI,"You are severly overweight")
Repeat=input("Do you want to make a new calculation?(Yes/No)")
if "No":
    print("Done")
    exit()
elif: "yes":
 

Что я должен добавить здесь или где-либо еще в коде?

Комментарии:

1. в jmkkkkkkkkkkkkk потому что мне нужно написать что-то, чтобы задать вопрос.

2. Пожалуйста, найдите в Интернете учебник по Python и прочитайте о циклических конструкциях.

Ответ №1:

Итак, ваш код немного испорчен в начале, позвольте мне исправить это очень быстро, вы должны удалить неправильно размещенный («)

 A="Welcome"
B="Done by: (Name) Jhon Smith"
C="Class: (Class) AB"
D="IT Final project - CP2"
E="A program that calculates the BMI and describes it as underweight, healthy, overweight, severly overweight, obese, severly obese"
 

Теперь, когда это сделано, главный вопрос.

Вам нужно использовать циклы while, которые будут выполняться до тех пор, пока условие не станет ложным. В этом случае мы хотим, чтобы он повторялся до тех пор, пока пользователь не введет «нет». Поэтому вам нужно будет обернуть свой код while 1: Вот этим: (Плюс некоторые изменения, чтобы код работал)

 A="Welcome"
B="Done by: Name"
C="Class: Class"
D="IT Final project - CP2"
E="A program that calculates the BMI and describes it as underweight, healthy, overweight, severly overweight, obese, severly obese"
print(A)
print(B)
print(C)
print(D)
print(E)



while 1:
  try:
    height = float(input("Enter height in meters: "))
    if height == 0:
      print("Error, input not valid")
      continue
  except Exception:
    print("Error, input not valid")
    continue
  try:
    weight = float(input("Enter weight in kg: "))
    if weight == 0:
      print("Error, input not valid")
      continue
  except Exception:
    print("Error, input not valid")
    continue
  
  bmi = (weight/height**2)
  
  if bmi <= 18.4:
    print(bmi, "You are underweight!")
  elif bmi <= 24.9:
    print(bmi, "You are healthy!")
  elif bmi <= 29.9:
    print(bmi, "You are overweight.")
  elif bmi <= 34.9:
    print(bmi, "You are severly overweight.")
  elif bmi <= 39.9:
    print(bmi, "You are obese.nYou are severly overweight.")
  
  while 1:
    end = input("Repeat? (Yes/No): ")
    if end.lower() == "yes":
      break
    elif end.lower() == "no":
      print("Done.")
      exit()
    else:
      print("Please type Yes/No")
```
This works perfect, if you need any help, post a comment!

PS: Press the check mark beside this answer to mark it as "helpful" / accept answer