Reputation: 23
I'm struggling with functions in python 3. How can I make the below code print out the result in degrees Celsius? Is the indentation wrong, or has it to do with me not defining Celsius outside the function in the first place? Does this mean I do not have to use functions for simple tasks or is it better to do so anyway?
print ("Welkom bij mijn Fahrenheit naar Celsius converteerprogramma!")
fahrenheit = int(input("Voer aantal graden Fahrenheit in "))
def converter_fahrenheit(fahrenheit):
celsius = (fahrenheit - 32) * 5/9
print (fahrenheit, "graden Fahrenheit is omgerekend ","%.2f" % celsius,"graden Celsius")
Upvotes: 1
Views: 214
Reputation: 1686
You seem to have misunderstood how functions work. When you write something like:
def converter_farenheit(farenheit):
some_code
The code inside doesn't actually get run until you call the function by calling it later in the program. You've already called a few functions like int
and input
and print
in your program, and using converter_farenheit
looks basically the same.
Further, if you want to get a value back out of a function, you need it to return
something, which passes it back out of the function allowing you to assign variables to its value, so converter_farenheit
should really look like
def converter_fahrenheit(fahrenheit):
return (fahrenheit - 32) * 5/9
Which means you can write
def converter_fahrenheit(fahrenheit):
return (fahrenheit - 32) * 5/9
fahrenheit = # some code to grab input goes here
celsius = converter_farenheit(farenheit)
Giving celsius
the value you expect it to. You asked whether you should avoid using functions for simple tasks, but in fact you should write as many simple functions as possible. As your programs get more complex, you'll find that lots of short, simple functions each doing a small, specific job are very easy for you to reason about and test.
Upvotes: 1
Reputation: 175
You can do this by defining your function converter_fahrenheit()
first then taking the input and finally calling the function after having the input. You haven't called the function to get the value of celsius
and it's the main issue.
The code should be like in this way:
def converter_farenheit(farenheit):
celsius=(farenheit-32)*5/9
print(farenheit,"is the temperature in farenheit and in Celsius ","%.2f" %celsius)
converter_farenheit(farenheit=int(input("Enter the temperature in Farenheit ")))
Please mark the indentation well too. :)
Upvotes: 1
Reputation: 53774
Quite a few changes needed, yes you had the indentation wrong for the print statement. But that's not all see how the code is ordered now
def converter_fahrenheit(fahrenheit):
celsius = (fahrenheit - 32) * 5/9
print (fahrenheit, "graden Fahrenheit is omgerekend ","%.2f" % celsius,"graden Celsius")
print ("Welkom bij mijn Fahrenheit naar Celsius converteerprogramma!")
fahrenheit = int(input("Voer aantal graden Fahrenheit in "))
converter_fahrenheit(fahrenheit)
Defining the function first, makes it possible for you to actually call the function without an error being thrown. Yes, you were not calling the function at all!
Upvotes: 1