4. Input/Output Assignment¶

Quick Overview of Day

Go over practice problem(s) from last day. Work on a first Python assignment, focused on input/output, data types and conditionals.

4.1. What Does This Program Do?¶

Remember that a single equal sign = is used to assign a value. Two equal signs == are used when comparing a value.

Note

Your teacher may choose to use the following examples as a class activity, by displaying the examples, and having you take a guess as to what you think each will do before running the code.

What will the following programs output? Why?

Can you spot the error in the following example? How could you fix it?

4.2. Being Careful with Data Types¶

Remember that when you are taking input from the user with the input() function, the value is always, always a string! If you need to do some kind of mathematical calculation with input from the user, you must convert the input into a number (either an int or a float). Recall that the functions available to you for type conversion are:

• int(x) to convert x to an integer
• float(x) to convert x to a floating point number (number with a decimal)
• str(x) to convert x to a string (a value surrounded by quotes)

4.3. Input/Output Sample Assignment¶

Note

You have now had a chance to practice a number of problems that involved taking in input from the user, performing some operation on that input, and printing out a result. This might be a nice time to put the knowledge you’ve gained into practice on a hand-in assignment. One possible assignment is given below.

You can either work directly in the textbook, or using Thonny. Either way, be sure to save your solution into your Computer Science 20 folder when you finish for the day!

Write a Python program that will convert degrees Celsius to degrees Fahrenheit AND from Fahrenheit to Celsius.

Your program should ask the user which conversion you’d like to do (F to C, or C to F), then to input a value in degrees Celsius/Fahrenheit, and should output the converted value in degrees Fahrenheit/Celsius.

For this first assignment, you don’t need to foolproof user input (in other words, you can assume that the user will enter just a number). This means that if the user were to enter in bob when you ask them for the temperature, your program will crash, and that is okay.

As this is the first Python assignment you are going to submit, the following notes might be useful:

# Temperature Conversion Assignment