[Tutor] temp conversion problem was: Re: (no subject)
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")
You could have had this return the menu as a string instead of printing it.
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":
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:
> 'p' print options
> 'c' convert from celsius
> 'f' convert from fahrenheit
> 'q' quit the program
It prints the menu
Then it prints options and reads the value
Then it prints that value and reads another value
which it sets choice to.
If your print_menu() function returned the menu as a string
(and was called get_menu) you could do
choice = input(get_menu())
or if it got the choice in the menu function it would look like
choice = get_menu_item()
Which is a lot simpler to read.
Author of the Learn to Program web site
More information about the Tutor