Functions and User Input |Section 3|Celestial Warrior

1 year ago
2

The next lecture will be an exercise and there will be more exercises as you progress through the course.

The exercises are difficult and sometimes tricky and I don't expect students to solve them all. Most students actually fail to solve the exercisesunless they are already familiar with Python.

The whole point of exercises is to force you to think about how to solve a problem independently. The next lecture following each exercise contains the solution where you can compare your approach.

Create a functionthat converts Celsius degrees toFahrenheit. The formula to convert Celsius to Fahrenheit isF= C 9/5 + 32.

In the previous exercises you used the interactive Udemy interface to write your code.For this exercise, please use your computertools (Python, editor, terminal,etc.). This will help you get used with Python in a real environment.

Once you are done check your solution against my solution which islocatedin the next lecture. Don't worry if your solution is not exactly the same as mine. As far as the solutions generate the same output, your solution is most likely correct.

def cel_to_fahr(c):
f = c * 9/5 + 32
return f
And here is an example of calling the function:

print(cel_to_fahr(10))

That would return 50.0 which means 10 degreeCelsius is equal to 50 degreeFahrenheit.

Create a function that takes any string as argument and returns the length of that string.

def string_length(mystring):
return len(mystring)
And here's an example of calling the function:

print(string_length("Hello"))

That would return 5.

Loading comments...