Vise
Vise

Reputation: 11

How should i go about using a user input in an equation like Fahrenheit to Celsius? When I run the code it says it is the conversion line

#taking temperature in fahrenheit
fahrenheit = float(input("Enter temperature degrees in fahrenheit:"))

#Coversion formula
conv_for = (input - 32) * 5/9

#calculation for celcius
celcius = conv_for
    print("%02f degrees in fahrenheit is equal to %02f degrees in celcius")

I am fairly new to coding so are their any other ways to improve this?

Upvotes: 0

Views: 65

Answers (3)

rb612
rb612

Reputation: 5563

In this line, you're taking the temperature the user enters and storing it in a variable called fahrenheit.

#taking temperature in fahrenheit
fahrenheit = float(input("Enter temperature degrees in fahrenheit:"))

So if the user typed in, say, 76, then fahrenheit would store the value 76. In this line, however, you're using input instead of fahrenheit.

#Coversion formula
conv_for = (input - 32) * 5/9

input is essentially a function that takes what the user types in and stores it. I think what you're actually wanting is, instead of input, to use fahrenheit. While we're at it, let's assign that to celsius instead of conv_for for clarity. Here's the corrected version of the above:

celsius = (fahrenheit - 32) * 5/9

Similarly, your print statement needs to specify which variables go in for each spot:

print(f"{fahrenheit} degrees in fahrenheit is equal to {celsius} degrees in celsius")

You could alternatively do it like you were doing before and it will then round at 2 decimal places (that is what 02 means):

print("%.02f degrees in fahrenheit is equal to %.02f degrees in celsius" % (fahrenheit, celsius))

Upvotes: 2

Albaraa
Albaraa

Reputation: 83

ok first thing indention is important in python so be careful,

# Taking temperature in fahrenheit
fahrenheit = float(input("Enter temperature degrees in fahrenheit:"))

# Calculation for celcius
celcius = (fahrenheit - 32) * 5/9

#Displaying the output
print("%s degrees in fahrenheit is equal to %s degrees in celcius"%(fahrenheit, celcius))

Upvotes: 0

Red
Red

Reputation: 27567

Here is the corrected code:

fahrenheit = float(input("Enter temperature degrees in fahrenheit:"))

#Coversion formula
conv_for = (fahrenheit - 32) * 5/9

#calculation for celcius
celcius = conv_for
print(f"{fahrenheit} degrees in fahrenheit is equal to {celcius} degrees in celcius")

Upvotes: 0

Related Questions