Featured Posts

CS50 Python , Nutrition Facts Table

Image
Nutrition Facts: Python Practice for Beginners Nutrition Facts Table for Python Practice Welcome to this comprehensive guide for Python beginners! If you are learning how to work with lists, dictionaries, and loops, this post will help you build practical skills using a real-world example: nutrition facts for fruits. Understanding how to organize and manipulate data is a key part of programming, and this exercise will give you hands-on experience. Below is a sample table of fruits and their calorie values, formatted as a Python list of dictionaries. This structure is ideal for coding exercises, projects, or even building your own nutrition calculator. You can expand this list, add new fruits, or use it as a foundation for more advanced Python tasks. Python List of Dictionaries Example: fruits = [ {'name': 'Apple', 'calories': 130}, {'name': 'Avocado', 'calories': 50}, {'name': 'Banana', 'ca...

Python 2.6.1.11 LAB: Operators and expressions

 A Simple Python Code to Evaluate the End Time

If you're taking PCAP - Programming Essentials In Python , you may have encountered this question in 2.6.1.11 LAB: Operators and expressions asking you for a code to evaluate end time. It goes like this:

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 an invalid time - the most important thing is that the code produce valid results for valid input data.

Test your code carefully. Hint: using the % operator may be the key to success.

Solution with Comments:

hour = int(input("Starting time (hours): ")) #12
mins = int(input("Starting time (minutes): ")) #17
dura = int(input("Event duration (minutes): ")) #59

The Code:

new_mins = mins + dura # Add a new variable for minutes to add the input minutes to the existing ones                                           (mins = 17 . dura = 59. new_mins = 17+59 = 76)

new_hours = new_mins // 60 # Add a new variable for the new hours. // 60 means to get how many                                                          hours (rounded) in the new given minutes (76 // 60 = 1)

final_hours = new_hours + hour # Add a new variable to get the result needed for the hours. It's an                                                                addition process to add the input hour (12) + the new hours rounded                                                         (1) will be (13)

final_minutes = new_mins % 60 #Add a new variable to get the result needed for the minutes. It's an                                                                addition process to get the remainder of the new minutes (76) which is (16). Why 16? one hour = 60 minutes. 76 - 60 (this's called remainder %) = 16

print(final_hours , ":" , final_minutes)


Comments

Popular posts from this blog

فرصتك للدراسة في ألمانيا: منحة ممولة بالكامل لطلاب الدراسات العليا

How to Deactivate Screen Reader in Kali Linux

Murphy's Law: Expect the Unexpected

Python 3.2.1.14 LAB: Essentials of the while loop

Data Analysis Roadmap 2026: From Excel Lover to Python-Powered Analyst