[Tutor] Fahrenheit to Celsius Conversion with if else statements
Alan Gauld
alan.gauld at yahoo.co.uk
Mon Jun 12 13:36:26 EDT 2017
On 12/06/17 15:17, William Gan wrote:
> print('Enter C for Celsius to Fahrenheit or F for Fahrenheit to Celsius.')
> unit = input('Enter C or F:')
> temp = int(input('Enter temperature:'))
>
> if unit == 'C':
Note this only t5ests for 'C' - ie capital C.
You might want to force the input to be uppercase first?
if unit.upper() == 'C':
> f = (temp + 32) * 9 / 5
> print(str(temp) + ' C is equivalent to ' + "%.2f" % f + ' F.')
> else:
> c = (temp - 32) * 5 / 9
> print(str(temp) + ' F is equivalent to ' + "%.2f" % c + ' C.')
>
> However, when I entered C, the else block was executed instead. The if block was skipped.
>
> Enter C for Celsius to Fahrenheit or F for Fahrenheit to Celsius.
>
> Enter C or F:c
Note you entered lowercase 'c' not 'C'.
Very different.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list