55.7k views
5 votes
The date June 10, 1960, is special because when it is written in the following format, the month times the day equals the year: 6/10/60 Design a program that asks the user to enter a month (in numeric form), a day, and a two digit year. The program should then determine whether the month times the day equals the year. If so, it should display a message saying the date is magic. Otherwise, it should display a message saying the date is not magic.

2. Running on a particular treadmill you burn 3.9 calories per minute. Write a program that uses a loop to display the number of calories burned after 10, 15, 20, 25, and 30 minutes.

User Liubiantao
by
5.4k points

1 Answer

5 votes

Answer for Question 1:

Code for the first question:

#section 1

# Prompt for accurate day month and year

while True:

try:

day = int(input("Enter the day of month: "))

if day > 31 or day < 1:

raise

try:

month = int(input("Enter the month in numeric form: "))

if month > 12 or month < 1:

raise

try:

year = int(input("Enter the year in two digit format: "))

if year > 99 or year < 0:

raise

else:

break

except:

print("Error: invalid year input.")

except:

print("Error: invalid month input.")

except:

print("Error: invalid day input.")

# We have a valid date, lets determine whether it's magic or not

#Section 2

# Calculate the mutliplication of day and month

dayxMonth = day * month

# Display the results to the screen

print()

print("The date ", day, "/", month, "/", year, end=" ")

# Calculates whether the entered date is a magic date

if dayxMonth == year:

print ("is a magic date.")

else:

print ("is not a magic date.")

Explanation for Question 1:

#section 1

The Try and Except block in combination with the while loop is used to ensure that all inputs are accurate if there is an error in the input, it prompts the user to enter a valid input and states the problem with the input.

Each try block contains an if statement for day, month and year respectively.

#Section 2

calculates the multiplication of day and month,

compares it with the year to find out if it's magic and finally prints the result to the screen

Answer for Question 2:

Code for the second question:

print("Time Calories Burned in minutes")

print("---- ---------")

for time in range(10,31,5):

cburn=3.9*time

print(time,cburn,sep=' ')

Explanation for Question 2:

  • for time in range(10,31,5):

A for loop is used to iterate over a range of values.

from 10 to 31 and the skipper is 5.

The skipper is how many numbers are skipped before the for lop takes another value from the range.

  • cburn=3.9*time

print(time,cburn,sep=' ')

the values provided by the range are 10, 15, 20, 25, and 30 minutes.

we then calculate the calories for each value and print then to the screen.

The date June 10, 1960, is special because when it is written in the following format-example-1
The date June 10, 1960, is special because when it is written in the following format-example-2
User Jadli
by
4.8k points