[Tutor] temp conversion problem was: Re: (no subject)

Alan Gauld alan.gauld at btinternet.com
Mon Feb 11 19:04:45 CET 2013

```Please always provide a meaningful subject line, it helps us find
related posts more easily in future and will stimulate more focused
responses than a no subject message (which implies we are dealing
with a newbie with no net experience and therefore requiring a lot of
time/effort to help)...

On 11/02/13 15:56, Pravya Reddy wrote:
> Can You Plaese help me with the code.

Try to give us a bit more clue about what you think is wrong. What did
you expect? What did you get? Otherwise we have to cut n paste the code
and run it and then guess what you think is the problem...

The more work we have to do the less likely we will bother.

> def print_options():
>      print ("Options:")
>      print (" 'p' print options")
>      print (" 'c' convert from celsius")
>      print (" 'f' convert from fahrenheit")
>      print (" 'q' quit the program")

Or you could have got the user input inside the function and returned
the choice directly.
Either would be better than this.

> def celsius_to_fahrenheit(c_temp):
>      return 9.0/5.0*c_temp+32
>
> def fahrenheit_to_celsius(f_temp):
>      return (f_temp - 32.0)*5.0/9.0
>
> choice = "p"

You could have printed the menu and got the users real choice here
rather than set a default that you never use...

> while choice != "q":
>      if choice == "c":
>          temp = input(input("Celsius temperature:"))
>          print ("Fahrenheit:",celsius_to_fahrenheit(temp))
>      elif choice == "f":
>          temp = input("Fahrenheit temperature:")
>          print ("Celsius:",fahrenheit_to_celsius(temp))
>      elif choice != "q":
>          print_options()

This prints the menu if the user did not choose f or q.
But wouldn't you want the menu displayed every time
round the loop?

>      choice = input(input("option:"))

Why two inputs?
This prints option and reads the input.
It then prints the option and reads another input.

> The Output i got is:
> Options:
>   'p' print options
>   'c' convert from celsius
>   'f' convert from fahrenheit
>   'q' quit the program
> option:c
> c

Then it prints options and reads the value
Then it prints that value and reads another value
which it sets choice to.

(and was called get_menu) you could do

or if it got the choice in the menu function it would look like

Which is a lot simpler to read.

HTH

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

```