The following exercises will help you to get more comfortable with implementing simple arithmetic programs and using strings to print messages. They will make you use a few new functions and train coding logic. Using JupyterLab, write each of the programs into their own .py file, use consistent and meaningful names for the files and write short comments into the files that describe what the programs do. Place the files in the directory $SCIPRO/02_unit/.
Make sure you execute the programs in all the different ways that we have gotten to know!
Exercise #02-02: Calculator Program
Write a program that asks the user to input two numbers. Then, perform and display the result of basic arithmetic operations (addition, subtraction, multiplication, division, floor division, remainder) on these numbers. Upon running the program, the output could look like this:
Enter the first number: 8
Enter the second number: 3
The sum is: 11
The difference is: 5
The product is: 24
The quotient is: 2.6666666666666665
The second number fits into the first number 2 times with remainder 2.
Exercise #02-03: Temperature Conversion
Write a program that asks the user to input a temperature in Celsius and then converts the temperature to Fahrenheit. The formula is:
Fahrenheit = (Celsius×9/5) + 32
The output could look like this:
Enter the temperature in Celsius: 25
The temperature in Fahrenheit is: 77.0
Exercise #02-04: Variable Swapping
Create a program that defines two variables (no user input anymore), x and y, prints their values, and then swaps their values without using a third variable.
The output could look like this:
x is 5 and y is 10.
Hold on a second, the values might just have been swapped:
Now, x is 10 and y is 5.
Exercise #02-05: Name Length
Ask the user to enter their first and last name. Then, display the total number of characters (including spaces) in their full name.
Tip: You can combine strings like "first_name" + " " + "last_name". To count the characters, use len.
The output could look like this:
Enter your first name: John
Enter your last name: Doe
Your full name has 8 characters.
Solutions
Exercise 02-02: Calculator Program
# Solution# Ask for user inputnum1 =float(input("Enter the first number: "))num2 =float(input("Enter the second number: "))# Perform calculationssum_result = num1 + num2difference = num1 - num2product = num1 * num2quotient = num1 / num2floor_division = num1 // num2remainder = num1 % num2# Display resultsprint("The sum is: "+str(sum_result))print("The difference is: "+str(difference))print("The product is: "+str(product))print("The quotient is: "+str(quotient))print("The second number fits into the first numer "+str(floor_division) +\" times with remainder "+str(remainder))
Exercise 02-03: Temperature Conversion
# Solution# Ask for temperature input in Celsiuscelsius =float(input("Enter the temperature in Celsius: "))# Convert to Fahrenheitfahrenheit = (celsius *9/5) +32# Display the resultprint("The temperature in Fahrenheit is: "+str(fahrenheit))
Exercise 02-04: Variable Swapping
# Solution# Define two variablesx =5y =10# Display initial valuesprint("x is "+str(x) +" and y is "+str(y) +".")# Swap the values without using a third variablex = x*yy = x/yx = x/y# Display the swapped valuesprint("Hold on a second, the values might just have been swapped:")print("Now, x is "+str(x) +" and y is "+str(y) +".")
Exercise 02-05: Name Length
# Solution# Ask for the user's first and last namefirst_name =input("Enter your first name: ")last_name =input("Enter your last name: ")# Calculate the length including spacesfull_name = first_name +" "+ last_namename_length =len(full_name)# Display the resultprint("Your full name has "+str(name_length) +" characters.")