python编写的简单温度转换程序的代码

将开发过程中常用的内容段记录起来,下面内容内容是关于python编写的简单温度转换程序的内容。 

def c2f(t):

def c2k(t):
	return t+273.15

def f2c(t):

def f2k(t):

def k2c(t):
	return t-273.15

def k2f(t):

def get_user_input():

	user_input = 0
	while type(user_input) != type(1.0): 
		user_input = raw_input("Enter degrees to convert: ")
		try:
			user_input = float(user_input)
		except:
			print user_input + " is not a valid entry"
	return user_input

def main():

	menu = "nTemperature Convertornn"+
	 	"1. Celsius to Fahrenheitn"+
		"2. Celsius to Kelvinn"+
		"3. Fahrenheit to Celsiusn"+
		"4. Fahrenheit to Kelvinn"+
		"5. Kelvin to Celsiusn"+
    		"6. Kelvin to Fahrenheitn"+
		"7. Quit"
		

	user_input = 0
	while user_input != 7: 
		print menu
		user_input = raw_input("Please enter a valid selection: ")

		try:
			user_input = int(user_input)
		except:
			print user_input + " is not a valid selction, please try againn"

		if user_input == 1:
			t = get_user_input()
			print str(t) + " degree Celsius is " + str((c2f(t))) + " degree Fahrenheit" 
		elif user_input == 2:
			t = get_user_input()
			print str(t) + " degree Celsius is " + str((c2k(t))) + " degree Kelvin" 
		elif user_input == 3:
			t = get_user_input()
			print str(t) + " degree Fahrenheit is " + str((f2c(t))) + " degree Celsius" 
		elif user_input == 4:
			t = get_user_input()
			print str(t) + " degree Fahrenheit is " + str((f2K(t))) + " degree Kelvin" 
		elif user_input == 5:
			t = get_user_input()
			print str(t) + " degree Kelvin is " + str((k2c(t))) + " degree Celsius" 
		elif user_input == 6:
			t = get_user_input()
			print str(t) + " degree Kelvin is " + str((k2f(t))) + " degree Fahrenheit" 
		elif user_input == 7:
			quit()
		else: 
			print str(user_input) + " is not a valid selection, please try againn"
	
if __name__ == "__main__":
	main()    复制代码
                                                                                                                                         


转载于:https://juejin.im/post/5c18992d6fb9a049ac791635

你可能感兴趣的:(python编写的简单温度转换程序的代码)