Python Temperature Converter

For students who are taking a programming course, may be restricted in what they can use, or have other unusual limitations on solving their task.

Python Temperature Converter

Postby nicnic2001 » Wed Sep 14, 2016 5:31 pm

Hello,

In Year 10 and just started GCSE Computing, my H/W was to make a simple Temperature Converter and I am editing my code using IDLE.

Upon running my code, my program says I have entered an invalid input (
Code: Select all
else: print ("Invalid Input")

I do not know why this happens and I would please like some help or tips.
I know there are many easier approaches to make this program but I'm only looking for the problem to my code.

Code: Select all
def f_to_c(f):
    return (f - 32) * 5 / 9
def c_to_f(c):
    return c * 9 / 5 + 32

choice = int(input("Please select which you would like to convert:\n Fahrenheit to Celsius: 1\n Celsius to Fahrenheit: 2\n\nChoice: " ))
val = int(input("Please enter degree in which you wish to convert: "))
if choice == "1":
    cdeg = f_to_c(val)
    print (val, "F", " equals ", cdeg, "C") 
elif choice == "2":
    fdeg = c_to_f(val)
    print (val, "C", " equals ", fdeg, "F")
else:
    print ("Invalid Input")
   





Any help appreciated,

Nick
Last edited by Yoriz on Wed Sep 14, 2016 5:59 pm, edited 1 time in total.
Reason: First post lock.
nicnic2001
 
Posts: 1
Joined: Wed Sep 14, 2016 5:24 pm

Re: Python Temperature Converter

Postby Yoriz » Wed Sep 14, 2016 6:01 pm

choice is the users input converted into an int, the if statements are then comparing this int to a string, change the comparisions to ints.
Code: Select all
if choice == "1":
to
Code: Select all
if choice == 1:
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Yoriz
 
Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to Homework

Who is online

Users browsing this forum: No registered users and 3 guests