2017-02-07 90 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

相關問題