1. Write a program that uses input to prompt a user for their name and then welcomes them:
#Prompt the user for their name
name = input("Enter your name: ")
#Print a welcome message with the user's name
print("Hello", name, "!", "Welcome to our program.")
When you run this program, it will display the message "Enter your name:" in the console. The user can type in their name, and after pressing Enter, the program will print a welcome message using their name.
2. Write a program to prompt the user for hours and rate per hour to compute gross pay.
# Prompt the user for the number of hours worked
hours = int(input("Enter the number of hours worked: "))
#Prompt the user for the hourly pay rate
rate = int(input("Enter the hourly pay rate: "))
# Calculate the gross pay
pay = hours * rate
#Display the gross pay
print("Pay: " pay)
In this program, we first use the input function to get the number of hours worked and the hourly pay rate from the user. We convert the input values to integer numbers using int() since pay rates and hours must be numbers.
Then, we calculate the gross pay by multiplying the hours worked by the hourly rate. Finally, we display the gross pay.
3. Assume that we execute the following assignment statements:
width = 17
height = 12.0
For each of the following expressions, write the value of the expression and the type (of the value of the expression).
1. width//2
2. width/2.0
3. height/3
4. 1 + 2 * 5
1. width // 2: This expression performs integer division (floor division) of width by 2. Since width is an integer (17), the result will also be an integer. So, the value is 8 (floor division of 17 by 2), and the type is int.
2. width / 2.0: Here, width is divided by 2.0, which is a float. When you perform any operation involving a float, the result is also a float. So, the value is 8.5 (division of 17 by 2.0), and the type is float.
3. height / 3: height is a float (12.0), and we're dividing it by 3, which is an integer. The result will be a float. So, the value is 4.0 (division of 12.0 by 3), and the type is float.
4. 1 + 2 * 5: This expression involves both addition and multiplication. According to the order of operations (PEMDAS/BODMAS), multiplication is performed before addition. So, 2 * 5 is evaluated first, resulting in 10, and then 1 + 10 is evaluated, resulting in 11. The value is 11, and the type is int.
4. Write a program which prompts the user for a Celsius temperature, convert the temperature to Fahrenheit, and print out the converted temperature
# Prompt the user for a Celsius temperature
celsius = float(input("Enter a temperature in Celsius: "))
# Convert Celsius to Fahrenheit
fahrenheit = (celsius * 9/5) + 32
# Print the converted temperature
print("The temperature in Fahrenheit is: ", fahrenheit,"°F")
In this program:
1. We use the input function to get user input, which is initially a string.
2. We convert the input to a floating-point number using float().
3. We then perform the conversion from Celsius to Fahrenheit using the formula (Celsius * 9/5) + 32.
4. Finally, we print out the converted temperature in Fahrenheit.
When you run this program, it will prompt you to enter a temperature in Celsius, and it will display the equivalent temperature in Fahrenheit.

Comments
Post a Comment