def main(): loops=0 maxentry=2 print('Select Conversion Type:') print('1 - Miles to kilometer') print('2 - Gallons to liters') print('3 - Pounds to kilograms') print('4 - Inches to centimeters') print('5 - Fahrenheit to Celsius') choice = float(input('Choose what value to convert:')) if (choice==1):return miles else: print('Invalid selection') miles=float(input('How many miles to convert? ')) while miles<0 and loops<maxentry: loops+=1 print('The value you have entered is invalid. You have', 3-loops, 'attempts left. Please try again: ') miles=float(input('How many miles to convert? ')) if loops==maxentry and miles<0: print('ERROR! Invalid entry after', maxentry+1, 'attempts.') else: milesToKm(miles) loops=0 #Farenheit F=float(input('How many degrees to convert? ')) while F>1000 and loops<maxentry: loops+=1 print('The value you have entered is invalid. You have', 3-loops, 'attempts left. Please try again: ') F=float(input('How many degrees to convert? ')) if loops==maxentry and F >= 1000: print('ERROR! Invalid entry after', maxentry+1, 'attempts.') else: FahToCel(F) loops=0 #gallons gallon=float(input('How many gallons to convert? ')) while gallon<0 and loops<maxentry: loops+=1 print('The value you have entered is invalid. You have', 3-loops, 'attempts left. Please try again: ') gallon=float(input('How many gallons to convert? ')) if loops==maxentry and gallon < 0: print('ERROR! Invalid entry after', maxentry+1, 'attempts.') else: GalToLit(gallon) loops=0 #pounds pound=float(input('How many pounds to convert? ')) while pound<0 and loops<maxentry: loops+=1 print('The value you have entered is invalid. You have', 3-loops, 'attempts left. Please try again: ') pound=float(input('How many pounds to convert? ')) if loops==maxentry and pound < 0: print('ERROR! Invalid entry after', maxentry+1, 'attempts.') else: PoundsToKg(pound) loops=0 #inches inch=float(input('How many inches to convert? ')) while inch<0 and loops<maxentry: loops+=1 print('The value you have entered is invalid. You have', 3-loops, 'attempts left. Please try again: ') inch=float(input('How many inches to convert? ')) if loops==maxentry and inch < 0: print('ERROR! Invalid entry after', maxentry+1, 'attempts.') else: InchesToCm(inch) loops=0 def milesToKm(miles): loops=0 kilo=miles*1.6 print('That is',format(kilo), 'kilometers.') def FahToCel(F): loops=0 C=(F-32)*(5/9) print('That is',format(C), 'Celsius.') def GalToLit(gallon): loops=0 liter=gallon*3.9 print('That is',format(liter), 'liters.') def PoundsToKg(pound): loops=0 kgram=pound*.45 print('That is',format(kgram), 'kilograms.') def InchesToCm(inch): loops=0 cmeter=inch*2.54 print('That is',format(cmeter), 'centimeters.') main()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question