Assignment #02

Unit 2

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

# Solution
# Ask for user input
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

# Perform calculations
sum_result = num1 + num2
difference = num1 - num2
product = num1 * num2
quotient = num1 / num2
floor_division = num1 // num2
remainder = num1 % num2

# Display results
print("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))
# Solution
# Ask for temperature input in Celsius
celsius = float(input("Enter the temperature in Celsius: "))

# Convert to Fahrenheit
fahrenheit = (celsius * 9/5) + 32

# Display the result
print("The temperature in Fahrenheit is: " + str(fahrenheit))
# Solution
# Define two variables
x = 5
y = 10

# Display initial values
print("x is " + str(x) + " and y is " + str(y) + ".")

# Swap the values without using a third variable
x = x*y
y = x/y
x = x/y

# Display the swapped values
print("Hold on a second, the values might just have been swapped:")
print("Now, x is " + str(x) + " and y is " + str(y) + ".")
# Solution
# Ask for the user's first and last name
first_name = input("Enter your first name: ")
last_name = input("Enter your last name: ")

# Calculate the length including spaces
full_name = first_name + " " + last_name
name_length = len(full_name)

# Display the result
print("Your full name has " + str(name_length) + " characters.")