2017-04-10 26 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