2017-02-07 62 views
-1

我已经尝试了一切。在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,"!" 
+0

你想达到什么目的?我想'loop'是你的意思,脚本在计算后重新运行? – ppasler

回答

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; 
    }; 
} 
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循环将执行该序列。正如你所看到的,每次应用程序都会要求一个新值,然后再次启动循环。

这里是你的代码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,"!" 

我强烈建议你更多地了解算法和编程,而不是仅仅使用一种语言。认识一门语言与使用该语言的程序员不同。

+0

真棒!!!!!!!!!谢谢。奇迹般有效。超级有用,与实际的代码和我的学习。再次感谢,非常感谢。 :) :) :) – jOTTO

相关问题