The Noobs of Python: Ep.4.0 - What's your Function / Define your Function

@anon75264233

User input in python3 is always a string unless you cast it to be an integer or a float.
I have updates comments in my conversion program.


#defice fahrenheit() function
def fahrenheit(T_in_celsius):
#calculates and returns the temperature in degrees Fahrenheit 
	return (T_in_celsius * 9 / 5) + 32

while True:
#ask for imput and assign it to x variable
	x = input("Please enter temperature in celsius: ")
#if there is no input, print goodbye message and end program
	if x == "":
		print("Goodbye")
		break
#cast float class on variable x and assign that new value to variable c
	c = float(x)
#call fahrenheit() function for input
	print(c,"C is equal to", fahrenheit(c),"F")

Also have a look below for further breakdown.


>>> x = input("please enter a number ")
please enter a number 5
>>> y = input("please enter a number ")
please enter a number 3.2
>>> type(x)
<class 'str'>
>>> type(y)
<class 'str'>

as you can see both inputs x and y are strings

>>> a = 5
>>> b = 2.98
>>> type(a)
<class 'int'>
>>> type(b)
<class 'float'>
>>>

a is an integer and b is a float

>>> c = a + b
>>> c
7.98
>>> type(c)
<class 'float'>
>>>

Here c clearly will be a float since it has a decimal point

>>> a = 5
>>> b = 2.2
>>> c = a * b
>>> print(c)
11.0
>>> type(c)
<class 'float'>
>>>

however here when we multiply float and integer we will get a float in return unless you cast it as a different class. See below:

>>> c = int(c)
>>> print(c)
11
>>> type(c)
<class 'int'>
>>>

best way to is to experiment in the python interpreter and see what values it will return.
See the introduction to The Noobs of Python

Also i have looked at that C++ code but i didn’t understand anything ;/

1 Like