Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

LAB

Objectives

  • improving the ability to use numbers, operators, and arithmetic operations in Python
  • using the print() function's formatting capabilities 
  • learning to express everyday-life phenomena in terms of programming language

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:

  • The input() function is used to prompt the user for the starting hour, starting minute, and duration in minutes.
  • The start_total_minutes variable is calculated by converting the starting hour to minutes and adding it to the starting minute.
  • The end_total_minutes variable is calculated by adding the start_total_minutes and the duration_minutes, and then taking the modulo 1440 (the number of minutes in a day) to handle cases where the end time is on the next day.
  • The end_hour and end_minute variables are calculated by converting the end_total_minutes to hours and minutes, respectively, using integer division and modulo.
  • Finally, the end time is printed in the format hh:mm, with leading zeros added if necessary using the {:02d} format specifier.

 

Key takeaways

  1. The print() function sends data to the console, while the input() function gets data from console.
  2. The input() function comes with an optional parameter: the prompt string. It allows you to write a message before the user input, e.g.:
    name = input("Enter your name: ")
    print("Hello, " + name + ". Nice to meet you!")
  3. When the input() function is called, the program's flow is stopped, the prompt symbol keeps blinking (it prompts the user to take action when the console is switched to input mode) until the user has entered an input and/or pressed the Enter key. 

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:

  • The input() function is used to prompt the user for a number. In this case, the user enters 2.
  • The int() function is used to convert the user's input (which is initially a string) to an integer. So, x now has the value 2.
  • The expression x * "5" is evaluated. Since x is an integer and "5" is a string, this expression will repeat the string "5" x times. So, the output will be the string "55".

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:

  • The input() function is used to prompt the user for a number. In this case, the user enters 2.
  • The value entered by the user is treated as a string and assigned to the variable x.
  • The type() function is then used to determine the data type of x. Since the value of x is a string (even though it represents a numerical value), the output will be <class 'str'>.