2017-11-10 9 views
0

私はこの質問が以前に尋ねられたことを理解していますが、実際の解決策を見つけることはできません。私はPythonを初めて使い、複数のBMIを入力できるBMI電卓を作成する必要がありますが、最初に正常にループバックすることはできません。私は2つの方法を試して、どちらも動作していません。ここでプログラムをループバックしてPythonで再起動させるにはどうすればよいですか?

は、私が最初のもののために持っているものだが、これは文句を言わない「のループではない継続」として実行: `

while True: 
    print("BMI Calculator") 

weight = float(input("\nPlease enter your weight in KG: ")) 
height = float(input("\nPlease enter your height in metres: ")) 
bmi = weight/(height*height) 

if bmi <= 18.5: 
    print("Your BMI is", bmi,"which means you are underweight.") 

elif bmi > 18.5 and bmi < 25: 
    print("Your BMI is: ", bmi, "which means you are normal") 

elif bmi > 25 and bmi < 30: 
    print("Your BMI is: ", bmi, "which means you are overweight") 

elif bmi > 30: 
    print("Your BMI is: ", bmi, "which means you are obese") 

else: 
    print("There was an error with your input, Sorry.") 

while True: 
    answer = input("Would you like to enter another? key y/n: ") 
    if answer in ("y", "n"): 
     break 
    print("Invalid Input") 
if answer == "y": 
    continue 
else: 
    input("\nPress the enter key to exit") 
    break 

`

私が持っているもう一つは、このですが、それプリントBMI電卓、その後停止します。 `

def start(): 
    print("\nBMI Calculator") 

weight = float(input("\nPlease enter your weight in KG: ")) 
height = float(input("\nPlease enter your height in metres: ")) 
bmi = weight/(height*height) 

if bmi <= 18.5: 
      print("Your BMI is", bmi,"which means you are underweight.") 

elif bmi > 18.5 and bmi < 25: 
      print("Your BMI is: ", bmi, "which means you are normal") 

elif bmi > 25 and bmi < 30: 
      print("Your BMI is: ", bmi, "which means you are overweight") 

elif bmi > 30: 
      print("Your BMI is: ", bmi, "which means you are obese") 

else: 
      print("There was an error with your input, Sorry.") 

answer = input("Would you like to enter another? key y/n: ") 
while answer == "y": 
     start() 
     answer = None 

if answer == "n": 
    input("\nPress the enter key to exit") 

`

+1

は実際にあなたのコードがどのように見えるか、インデントを台無しに取得したものをということですか? –

+0

ループ内にすべてのコードを入れて、ユーザーがそう言ったときに抜け出す必要があります。 – lpozo

+0

インデントが台無し –

答えて

4

ここで問題となるのは、インデントレベル、Lauraです。連続する行にすべて同じインデントがあると(PythonはCやJavaとは異なり、ブロックがカッコで囲まれて区切られている)、何かがコードブロック内にあることをPythonは認識しています。

if answer not in ("y", "n"): 
    print("Invalid Input") 
    break 

if answer in ("y", "n"): 
    break 
print("Invalid Input") 

:私はからブールテストと命令順序を変更したコードのこのスニペットでは

while True: 
    print("BMI Calculator") 

    weight = float(input("\nPlease enter your weight in KG: ")) 
    height = float(input("\nPlease enter your height in metres: ")) 
    bmi = weight/(height*height) 

    if bmi <= 18.5: 
     print("Your BMI is", bmi,"which means you are underweight.") 

    elif bmi > 18.5 and bmi < 25: 
     print("Your BMI is: ", bmi, "which means you are normal") 

    elif bmi > 25 and bmi < 30: 
     print("Your BMI is: ", bmi, "which means you are overweight") 

    elif bmi > 30: 
     print("Your BMI is: ", bmi, "which means you are obese") 

    else: 
     print("There was an error with your input, Sorry.") 

    answer = input("Would you like to enter another? key y/n: ") 

    if answer not in ("y", "n"): 
     print("Invalid Input") 
     break 

    if answer == "y": 
     continue 
    else: 
     input("\nPress the enter key to exit") 
     break 

あなたのコードは次のようになります。

ループからを壊すと、followiそのループ内のコード行は実行されません。また、回答が( "y"、 "n")になるので、あなたが行っていた比較は常にTrueを返します。私は最後のループも削除しました。なぜなら、それはもっと意味があるからです。

第2のコードは、start()が機能しているのは、インデントレベルのためにprint("\nBMI Calculator")です。あなたはwhileループにはいいいえ質問を入れないでする必要がある第二の例のように役立ちます:)

+0

ありがとう:)それは今働きます。 –

+0

あなたは大歓迎です;) –

0

希望は見えます。このようなものはうまくいくはずです。

def start(): 
    print("\nBMI Calculator") 

    while True: 
     weight = float(input("\nPlease enter your weight in KG: ")) 
     height = float(input("\nPlease enter your height in metres: ")) 
     bmi = weight/(height*height) 

     if bmi <= 18.5: 
      print("Your BMI is", bmi,"which means you are underweight.") 
     elif bmi > 18.5 and bmi < 25: 
      print("Your BMI is: ", bmi, "which means you are normal") 
     elif bmi > 25 and bmi < 30: 
      print("Your BMI is: ", bmi, "which means you are overweight") 
     elif bmi > 30: 
      print("Your BMI is: ", bmi, "which means you are obese") 
     else: 
      print("There was an error with your input, Sorry.") 
     answer = input("Would you like to enter another? key y/n: ") 
     while answer == "y": 
      start() 
      answer = None 
     if answer == "n": 
      exit() 

start() 

私はY/Nの質問にELSE文を入れていないが、それは簡単な修正する必要がありますが、それが正常に動作します前に、あなたが書いた文のように見えます。

0

最初の事:

インデントの問題の原因直面している:

セカンドシング:代わりに

第二の機能に関しては、私はあなたのためにいくつかの提案を持っていますprintの場合はreturnです。

一般的には、入力を受け取り、 何かを返すことです。 returnステートメントは関数を終了させ、 をその呼び出し元に戻します。

ここで.format()方法

は、いくつかの変更を加えた2番目のコードであるのpythonを使用します。

def start(): 
    print("\nBMI Calculator") 
    weight = float(input("\nPlease enter your weight in KG: ")) 
    height = float(input("\nPlease enter your height in metres: ")) 
    bmi = weight/(height*height) 
    if bmi <= 18.5: 
     return "Your BMI is : {} which means you are underweight".format(bmi) 
    elif bmi > 18.5 and bmi < 25: 
     return "Your BMI is : {} which means you are normal".format(bmi) 
    elif bmi > 25 and bmi < 30: 
     return "Your BMI is : {} which means you are overweight".format(bmi) 
    elif bmi > 30: 
     return "Your BMI is : {} which means you are obese".format(bmi) 
    else: 
     return "There was an error with your input, Sorry." 

print(start()) 

answer = input("Would you like to enter another? key y/n: ") 
if answer == "y": 
    start() 
elif answer == "n": 
    input("\nPress the enter key to exit") 
関連する問題