如何选择退出选项,我如何保持程序运行

时间:2017-02-07 12:28:52

标签: loops

我已经尝试了一切。在线测试python小提琴。这是我能得到的,它可以工作,但不会循环,我可以按5以上的任何东西退出/谢谢你使用jys面积计算器。

print "Welcome to Jys area calculator!"
print "What is your name?"
name = raw_input() 
print "Hi",name, ",please select a formula using the letter it represents or press 6 to quit. Your options are:"
mylist = ['1 for square', '2 for rectangle', '3 for circle', '4 for triangle','5 for elipse']
for elem in mylist:
        print elem
shape = input()
if shape == 1:
        print "Okay",name, ",to work out area of square first enter length and then breadth of square"
        a = input()
        b = input()
        c=a*b
        print "The area of your square is",c
elif shape == 2:
        print "Okay",name, ",to work out area of rectangle first enter length and then breadth of rectangle"
        a = input()
        b = input()
        c=a*b
        print "The area of your rectangle is",c
elif shape == 3:
        print "Okay",name, ",to work out area of a circle enter the radius"
        r= input()
        c= 3.14159265359*(r*r)
        print "The area of your circle is",c  
elif shape == 4:
        print "Okay",name, ",to work out area(c) of triangle first enter height(h) then base(b) length"
        h= input()
        b= input()
        c=0.5*h*b
        print "The area of your triangle is",c
elif shape== 5:
        print "Okay",name, ",to work out area of ellipse first enter length then breadth."
        a= input ()
        b= input ()
        c= 3.14159265359*a*b
        print "The area of your elipse is",c
else:
    print "Thank you for using Jys area calculator",name,"!"

2 个答案:

答案 0 :(得分:0)

好吧,我没有看到任何要求下一个号码的循环?我必须承认我不懂蟒蛇小提琴,但在大多数语言中你可以这样写:

bool Looping=true;
while(Looping)
{
  shape=input();
  if shape==1
    calc and display
  if shape==2
    calc and display
  else
  {
    print "bye bye!";
    Looping=false;
  };
}  

答案 1 :(得分:0)

  

我已经尝试了一切

关于循环的最基本概念是循环运算符本身,在代码中我只看到一个简单的输入加上一些ifs / else if。循环在哪里?

你的逻辑应该是(伪逻辑)

input = get_value()

while(input not equal 6) {
  if(input equal 1) { .... square logic }
  else if(input equal 2) { ... }
  ...
  input = get_value()
}
print "Thanks for using the application" 

while循环将执行此序列,直到用户将值设置为6。正如您所看到的,每次应用程序在再次启动循环之前都会请求新值。

以下是带有while循环的代码:

print "Welcome to Jys area calculator!"
print "What is your name?"
name = raw_input() 
print "Hi",name, ",please select a formula using the letter it represents or press 6 to quit. Your options are:"
mylist = ['1 for square', '2 for rectangle', '3 for circle', '4 for triangle','5 for elipse']
for elem in mylist:
        print elem

shape = input()

while shape != 6:
    if shape == 1:
            print "Okay",name, ",to work out area of square first enter length and then breadth of square"
            a = input()
            b = input()
            c=a*b
            print "The area of your square is",c
    elif shape == 2:
            print "Okay",name, ",to work out area of rectangle first enter length and then breadth of rectangle"
            a = input()
            b = input()
            c=a*b
            print "The area of your rectangle is",c
    elif shape == 3:
            print "Okay",name, ",to work out area of a circle enter the radius"
            r= input()
            c= 3.14159265359*(r*r)
            print "The area of your circle is",c  
    elif shape == 4:
            print "Okay",name, ",to work out area(c) of triangle first enter height(h) then base(b) length"
            h= input()
            b= input()
            c=0.5*h*b
            print "The area of your triangle is",c
    elif shape== 5:
            print "Okay",name, ",to work out area of ellipse first enter length then breadth."
            a= input ()
            b= input ()
            c= 3.14159265359*a*b
            print "The area of your elipse is",c

    shape = input()

print "Thank you for using Jys area calculator",name,"!"

我强烈建议您了解有关算法和编程的更多信息,而不仅仅是使用语言。了解一种语言与成为该语言的程序员并不相同。