LAB
Objectives
Scenario
Your task is to prepare a simple code able to evaluate the end time of a period of time, given as a number of minutes (it could be arbitrarily large). The start time is given as a pair of hours (0..23) and minutes (0..59). The result has to be printed to the console.
For example, if an event starts at 12:17 and lasts 59 minutes, it will end at 13:16.
Don't worry about any imperfections in your code - it's okay if it accepts the invalid time - the most important thing is that the code produce valid results for valid input dat.
Test Data
Sample input:
12
17
59
Expected output: 13:16
Sample input:
23
58
642
Expected output: 10:40
Sample input:
0
1
2939
Expected output: 1:0
Used ChatGPT for code:
start_hour = int(input("Enter the starting hour (0-23): "))
start_minute = int(input("Enter the starting minute (0-59): "))
duration_minutes = int(input("Enter the duration in minutes: "))
start_total_minutes = start_hour * 60 + start_minute
end_total_minutes = (start_total_minutes + duration_minutes) % 1440
end_hour = end_total_minutes // 60
end_minute = end_total_minutes % 60
print("End time: {:02d}:{:02d}".format(end_hour, end_minute))
Explanation:
Key takeaways
NOTE
You can test the functionality of the input() function in its full scope locally on your machine. For resource optimization reasons, we have limited the maximum program execution time in Edube to a few seconds. Go to the Sandbox, copy-paste the above snippet, run the program, and do nothing - just wait a few seconds to see what happens. Your program should be stopped automatically after a short moment. Now open IDLE, and run the same program there - can you see the difference?
Tip: the above-mentioned feature of the input() function can be used to prompt the user to end a program. Look at the code below:
name = input("Enter your name: ")
print("Hello, " + name + ". Nice to meet you!")
print("\nPress Enter to end the program.")
input()
print("THE END.")
4. The result of th einput() function is a string. You can add strings to each other using the concatenation (+) operator. Check out this code:
num_1 = input("Enter the first number: ") # Enter 12
num_2 = input("Enter the second number: ") # Enter 21
print(num_1 + num_2) # the program returns 1221
5. You can also multiply (* - replication) strings, e.g.:
my_input = input("Enter something: ") # Example input: hello
print(my_input * 3) # Expected output: hellohellohello
Exercise 1
What is the output of the following snippet?
x = int(input("Enter a number: ")) # The user enters 2
print(x * "5")
55
If the user enters 2 as the input, the output of the snippet will be the string "55". Here's why:
Exercise 2
What is the expected output of the following snippet?
x = input("Enter a number: ") # The user enters 2
print(type(x))
<class 'str'>
Here's why: