Home > Net >  Learning python, I'm trying to make a fahrenheit conversion using a defined function
Learning python, I'm trying to make a fahrenheit conversion using a defined function

Time:04-13

celsius = input("Please enter degrees in celcius: ")
fahrenheit = 0

def f_conversion(celsius):
    ((celsius * 1.8)   32 == fahrenheit)



print("Your temp in fahrenheit is: "   str(fahrenheit))

Any clues on to why this isnt working out?

CodePudding user response:

First, there is no need to initialize variables in python. So setting fahrenheit to zero right away is unnecessary.

Second, in the function itself, you are making an equality call. == checks for equality while = sets variable values. Moreover, you are doing so backwards, fahrenheit would be on the left and assigned with = the result of the conversion.

Your script might instead look something like this:

celsius = float(input("Please enter degrees in celcius: "))

def conversion(celsius):
    return ((celsius * 9/5)  32)

result = conversion(celsius)

print (f"Your temp in fahrenheit is {result}")

CodePudding user response:

Try using:

celsius = int(input("Please enter degrees in celcius: "))
fahrenheit = (celsius * 1.8)   32
print("Your temp in fahrenheit is: "   str(fahrenheit))

A couple of pointers here:

  1. using the input() function returns the user input as a string and not an integer, so you have to use int(input()) to bind the variable celsius to the input as an integer.

  2. you do not need to write the conversion as a function. It is just a numeric callculation that Python can evaluate as an expression.

  3. in your code, you never actually call the function you wrote, you only defined it.

Hope this helps, and I would echo Thierry Lathuille's suggestion of working through a basic course. It won't take too long and it will get you off to al flying start.

CodePudding user response:

# Remember input always returns a string
celsius = input("Please enter degrees in celcius: ")
# celsius identifies a location im memory which contains a string
celsius = float(celsius)
# Now the name celsius locates memory of type float
# fahrenheit = 0 # A global value not needed here.
# fahrenheit identifies a location im memory which contains an integer = 0

def f_conversion(celsius):
    fahrenheit = (celsius * 1.8)   32  # Computes the conversion
    return fahrenheit


print("Your temp in fahrenheit is: "   str(f_conversion(celsius)))
  • Related