Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Goto或python中的任何替代方案_Python_Loops - Fatal编程技术网

Goto或python中的任何替代方案

Goto或python中的任何替代方案,python,loops,Python,Loops,需要使用label/goto的substation-用于python中的C、..等语言 所以基本上,我是python的新手(老实说,是编程新手),我一直在尝试超基本的东西,遇到了一个“路障” 打印(“你好用户!”) 打印(“欢迎使用您的体重指数计算器”) 打印(“请选择您的测量系统(回答A或B):”) 打印(“A)英寸和磅”) 打印(“B)米和千克”) ans=str(输入(“输入:A或B”)) 如果ans==“B”: h=浮动(输入(“请输入您的高度(米)”) w=浮动(输入(“请输入您的体重

需要使用label/goto的substation-用于python中的C、..等语言

所以基本上,我是python的新手(老实说,是编程新手),我一直在尝试超基本的东西,遇到了一个“路障”

打印(“你好用户!”)
打印(“欢迎使用您的体重指数计算器”)
打印(“请选择您的测量系统(回答A或B):”)
打印(“A)英寸和磅”)
打印(“B)米和千克”)
ans=str(输入(“输入:A或B”))
如果ans==“B”:
h=浮动(输入(“请输入您的高度(米)”)
w=浮动(输入(“请输入您的体重(千克)”)
体重指数=w/(h**2)
打印(“您的体重指数为:”)
打印(bmi)
如果体重指数<18.5:
打印(“您属于体重不足类别。”)

elif 18.5goto
开始你的编程生涯,这就是你如何写出完美的
意大利面代码的方法

让我们在这里回顾一下您的用例,您想回到询问用户是否没有给您预期的输入,为什么不使用循环而不是
goto

ans = "unexpected"
while(ans != "A" and ans != "B"):
  print("Hello User!")
  print("Welcome to your BMI Calculator.")
  print("Please choose your system of measurement (Answer in terms of A or B):")
  print("A)Inches and Pounds")
  print("B)Meters and Kilograms")
  ans = str(input("Input:A or B"))

if ans =="B":
    h = float(input("Please enter your height (in meters)"))
    w = float(input("Please enter your weight (in kilograms)"))
    bmi = w/(h**2)
    print("Your BMI is:")
    print(bmi)
    if bmi < 18.5:
         print("You are in the UNDERWEIGHT category.")
    elif 18.5 < bmi <24.9:
        print("You are in the HEALTHY-WEIGHT category.")
    else:
        print("You are in the OVERWEIGHT category.")
    print("THANK YOU FOR YOUR TIME.")
elif ans =="A":
    h = float(input("Please enter your height (in inches)"))
    w = float(input("Please enter your weight (in pounds)"))
    bmi = (w*0.453592)/((h*0.0254)**2)
    print("Your BMI is:")
    print(bmi)
    if bmi < 18.5:
        print("You are in the UNDERWEIGHT category.")
    elif 18.5 < bmi <24.9:
        print("You are in the HEALTHY-WEIGHT category.")
    else:
        print("You are in the OVERWEIGHT category.")
    print("THANK YOU FOR YOUR TIME.")

ans=“意外”
而(ans!=“A”和ans!=“B”):
打印(“你好用户!”)
打印(“欢迎使用您的体重指数计算器”)
打印(“请选择您的测量系统(回答A或B):”)
打印(“A)英寸和磅”)
打印(“B)米和千克”)
ans=str(输入(“输入:A或B”))
如果ans==“B”:
h=浮动(输入(“请输入您的高度(米)”)
w=浮动(输入(“请输入您的体重(千克)”)
体重指数=w/(h**2)
打印(“您的体重指数为:”)
打印(bmi)
如果体重指数<18.5:
打印(“您属于体重不足类别。”)

elif 18.5您需要使用循环,而不是转到。无论如何,Python没有goto。自从年前(60多年前!)以来,我们极力避免使用goto。将所有内容都放在函数中,在else语句中调用该函数。@hansefranz true,但这将是一个递归函数。当你只是在学习编程时,这不是最好的主意。如果用户输入的高度为0(零),会发生什么?此外,每个条件块的最后7行是相同的,因此您应该将它们移动到一个公共位置。Python中没有等效的。如上所述,Python是在结构化编程成为一种事物之后(即在过去60年内…)出现的一种语言。一个程序的流程是由1)序列控制的——事物在一个序列中逐行移动),2)选择——这是条件分支,所以如果……elif……else 3)迭代/循环——循环和while循环也是如此4)递归。在这里,您可能只需要一个while循环。
ans = "unexpected"
while(ans != "A" and ans != "B"):
  print("Hello User!")
  print("Welcome to your BMI Calculator.")
  print("Please choose your system of measurement (Answer in terms of A or B):")
  print("A)Inches and Pounds")
  print("B)Meters and Kilograms")
  ans = str(input("Input:A or B"))

if ans =="B":
    h = float(input("Please enter your height (in meters)"))
    w = float(input("Please enter your weight (in kilograms)"))
    bmi = w/(h**2)
    print("Your BMI is:")
    print(bmi)
    if bmi < 18.5:
         print("You are in the UNDERWEIGHT category.")
    elif 18.5 < bmi <24.9:
        print("You are in the HEALTHY-WEIGHT category.")
    else:
        print("You are in the OVERWEIGHT category.")
    print("THANK YOU FOR YOUR TIME.")
elif ans =="A":
    h = float(input("Please enter your height (in inches)"))
    w = float(input("Please enter your weight (in pounds)"))
    bmi = (w*0.453592)/((h*0.0254)**2)
    print("Your BMI is:")
    print(bmi)
    if bmi < 18.5:
        print("You are in the UNDERWEIGHT category.")
    elif 18.5 < bmi <24.9:
        print("You are in the HEALTHY-WEIGHT category.")
    else:
        print("You are in the OVERWEIGHT category.")
    print("THANK YOU FOR YOUR TIME.")