temperature?
i am a bit stuck on this, i need to call the first 2 functions for the output in the 3rd one. The user has to should be able to continue until they want to stop converting and they should be asked on which way the conversion is to be performed?
def fahrenheit2Celsius(fahrenheit):
return (fahrenheit - 32) * 5.0 / 9.0
def celsius2Fahrenheit(celsius):
return 9.0 / 5.0 * celsius + 32
def fahrenheit2celsius():
temeperature= input("please enter the temperature to convert: ")
fahrenheitorcelsius=input("1=farenheit/2=celsius")
if fahrenheitorcelsius==1:
fahrenheit2Celsius(fahrenheit)
elif fahrenheitorcelsius==2
celsius2Fahrenheit(celsius)
gangster88
Junior Poster in Training
65 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
how come when i run this code the loop does not work?
def fahrenheit2Celsius( fahrenheit ):
return ( fahrenheit - 32 ) * 5.0 / 9.0
def celsius2Fahrenheit( celsius ):
return 9.0 / 5.0 * celsius + 32
def fahrenheit2celsius():
temperature = input( "Please enter the temperature to convert: " )
fahrenheitorcelsius = input( "1 = farenheit / 2 = celsius: " )
if fahrenheitorcelsius == 1:
print temperature, "in Fahrenheit is", fahrenheit2Celsius( temperature ), "Celsius"
elif fahrenheitorcelsius == 2:
print temperature, "in Celsius is", celsius2Fahrenheit( temperature ), "Fahrenheit"
def main():
fahrenheit2celsius()
for i in range(10000):
cont = raw_input('Would you like to continue converting? (yes/no): ')
if cont == 'yes' or cont == 'Yes':
fahrenheit2celsius()
else:
break
fahrenheit2celsius()
gangster88
Junior Poster in Training
65 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
The function main() can be simplified ...
def main():
while True:
fahrenheit2celsius()
cont = raw_input('Continue converting? (yes/no): ')
if 'n' in cont.lower():
break
while True forms an endless loop and the n in no or NO will break out of the loop.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
This will give your program a certain eloquence:
# convert temperatures using Python2 or Python3
def f2c( fahrenheit ):
"""convert Fahrenheit to Celsius"""
return ( fahrenheit - 32 ) * 5.0 / 9.0
def c2f( celsius ):
"""convert Celsius to Fahrenheit"""
return 9.0 / 5.0 * celsius + 32
def main(info):
print( info )
while True:
t = str(input( "Enter the temperature: " )).lower()
if 'q' in t:
break
if 'f' in t:
# remove last char, convert to number
num = float(t[:-1])
celsius = f2c(num)
print( "%0.1f F = %0.1f C" % (num, celsius) )
elif 'c' in t:
# remove last char, convert to number
num = float(t[:-1])
fahrenheit = c2f(num)
print( "%0.1f C = %0.1f F" % (num, fahrenheit) )
info = """\
Enter the temperature with the unit of measurement,
for instance ...
100F will give the temperature in Celsius
100C will give the temperature in Fahrenheit
(enter just q to quit the program)
"""
# program starts here
main(info)
Not totally fool proof, but what is?
Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212