[Tutor] Fahrenheit to Celsius Conversion with if else statements

William Gan ganwilliam at outlook.com
Mon Jun 12 14:16:15 EDT 2017


Good day Alan,

Very much thanks for your guidance.

I have added or 'c' to the if statement. That is resolved.

Through that correction I discovered my C to F code was wrong. The + 32 is supposed to be executed at the end.

Thanks again. Cheers



-----Original Message-----
From: Alan Gauld [mailto:alan.gauld at yahoo.co.uk] 
Sent: Tuesday, June 13, 2017 1:36 AM
To: tutor at python.org
Subject: Re: [Tutor] Fahrenheit to Celsius Conversion with if else statements

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