我不知道如何在我的代码中添加while语句

时间:2015-07-25 20:41:37

标签: python

嘿那些喜欢标题的人说我不太确定如何在我的代码中添加一段True语句。到目前为止,在程序计算出答案后,它会返回到命令提示符,而不是菜单,这是我想要它做的。任何帮助是极大的赞赏。感谢

1) Area (Square)
2) Area (Rectangle)
3) Area (Circle)
4) Perimeter (Square)
5) Perimeter (Rectangle)
6) Perimeter (Circle)
7) Exit Program
""")

Choice = input ("1,2,3,4,5,6 OR 7? ")

while True:
    if Choice == "1":
        print ("You have chosen area (Square)")
        length = float(input("input Length?"))
        print ("Area is:" ,  length**2.0)
    elif Choice == "2":
        print ("You have chosen area (Rectangle)")
        length = float (input("input length?"))
        width = float (input("input width?"))
        print ("Area is:" , length*width)
    elif Choice == "3":
        print ("You have chosen area (Circle)")
        radius = float (input("input radius?"))
        print ("Area is:" , (radius**2.0)*3.14)
    elif Choice == "4":
        print ("You have chosen perimeter (Square)")
        length = float(input("Input length?"))
        print ("Perimeter is:" , length*4.0)
    elif Choice == "5":
        print ("You have chosen perimeter (Rectangle)")
        width = float(input("Input width?"))
        length = float(input("Input length?"))
        print ("Perimeter is:" , width*2.0+length*2.0)
    elif Choice == "6":
        print ("You have chosen perimeter (Circle)")
        radius = float(input("Input radius?"))
        print ("Perimeter is:" , 2.0*3.14*radius)
    elif Choice == "7":
        print ("You have chosen to exit")
    exit()
    break

2 个答案:

答案 0 :(得分:2)

你的休息是在循环结束时,所以这就是你告诉程序要做的事情:

while True:
    //Choice stuff
    exit the loop

breakexit()移到elif Choice == "7":内,只有在选择了选项7时才会执行该代码。

您实际上只需要一个转义功能。我倾向于不使用exit()而只使用break - 如果这是程序的结束,它将会退出。如果您有下面的代码,它将被执行。好的做法是保持一致。

答案 1 :(得分:1)

但是你必须将@Override public boolean onMarkerClick (Marker marker) { Model hotelModel = markerMap.get(marker); if(hotelModel != null) { Log.d("test", "Hotel "+hotelModel.getTitle()+" is clicked"); } } 放入exit声明中。并且else不是必需的。