2017-04-10 69 views
0

我在試圖弄清楚如何循環我的程序以返回開始輸入值的位置時遇到問題。任何建議都會有所幫助,我是編程新手。 (python 2.17,wing ide 101 6.0)我如何循環我的程序?

#This program converts the users temperature in celsius to fahenheit 
import math 

print "X" * 46 
print "Temperature Converter" 
print "Press 1 for Celsius to Fahrenheit" 
print "Press 2 for Fahrenheit to Celsius" 
print "Press 3 for Celsius to Kelvin" 
print "Press 4 for Fahrenheit to Kelvin" 
print "X" * 46 


choice = input("Enter a value ") 

#converts celsius to fahrenheit 
if choice == 1: 
    print "1. Celsius to Fahrenheit" 
    CtoF = input("Temperature in Celsius? ") 
    tempF = round((CtoF * 1.8) + 32) 
    print "Your temperature in Fahrenheit is", tempF, "°" 

#converts fahrenheit to celsius 
if choice == 2: 
    print "2. Fahrenheit to Celsius" 
    FtoC = input("Temperature in Celsius? ") 
    tempC = round((FtoC - 32)/1.8) 
    print "Your temperature in Celsius is", tempC, "°" 

#Converts celsius to kelvin 
if choice == 3: 
    print "3. Celsius to Kelvin" 
    CtoK = input("Temperature in Celsius? ") 
    tempK = round(CtoK + 273.15) 
    print "Your temperature in Kelvin is", tempK 

#converts fahrenheit to celsius, then to kelvin 
if choice == 4: 
    print "4. Fahrenheit to Kelvin" 
    FtoK = input("Temperature in Fahrenheit? ") 
    FtokC = ((FtoK - 32)/1.8) 
    Ftok = round(FtokC + 273.15) 
    print "Your temperature in Kelvin is", Ftok 
+3

閱讀上'while'循環和'for'循環 – Wright

回答

0

例如,您可以在開始消息中添加「Enter 0 to exit」。
然後,您將從choice = input(...)(包括此行)開始的所有內容都放入while (True):循環中。
最後,在最後加上if choice == 0: break,你很好。

+0

@ QuintenChokrev埃文斯你應該增加,而內循環這條線。此外,你當然應該從換行寫'break'。 **還**,不要讓我猜 - 寫你的錯誤。僅供參考,Python代碼中可能有超過9000個不同的錯誤。所以下次寫錯誤 – mekkanizer

0

接近你的代碼一樣,電腦會:

1)考慮,你想開始循環

2)設置你的循環的條件。計算機不知道你想要什麼,除非你明確地傳達它;你想要它永遠循環?直到發生事情?溝通如何,如何,何時停止。

我建議你在While循環閱讀的Python文檔;)

+0

感謝您的建議,我設法使其工作。 – Quinten