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 LAB 2.8.1.4 Reading ints safely

 Reading ints safely

Reading ints safely
Reading ints safely

Objectives

  • improving the student's skills in defining functions;
  • using exceptions in order to provide a safe input environment.

Scenario

Your task is to write a function able to input integer values and to check if they are within a specified range.

LAB sandbox
LAB Sandbox

The function should:

accept three arguments: a prompt, a low acceptable limit, and a high acceptable limit;

if the user enters a string that is not an integer value, the function should emit the message Error: wrong input, and ask the user to input the value again;

if the user enters a number which falls outside the specified range, the function should emit the message Error: the value is not within permitted range (min..max) and ask the user to input the value again;

if the input value is valid, return it as a result.

Test data

Test your code carefully.

This is how the function should react to the user's input:

Enter a number from -10 to 10: 100

Error: the value is not within permitted range (-10..10)

Enter a number from -10 to 10: asd

Error: wrong input

Enter number from -10 to 10: 1

The number is: 1

Solution:

def read_int(prompt, min, max):

    ok = False
    while not ok:     #---> True
        try:    
            value = int(input(prompt))
            ok = True
        except ValueError:
            print("Error: Wrong Input")
        if ok:
            ok = value >= min and value <= max
        if not ok:
            print("Error: The value is not within range(" + str(min) + ".." + str(max) + ")")
    return value
v = read_int("Enter a number from -10 to 10: ", -10, 10)
print("The number is:", v)




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