Python Forum
Homework on conditional execution
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Homework on conditional execution
#3
You're repeating your code. The cast to float() could be done once.
The "elif float(score) < 0.0" branch is never executed.
The else block also not.

If the score is below 0.6, F is printed.
This is also for negative values True.
The question is, do you need or want to check the range?
If you don't check form minium and maximum, you could remove some parts:

score = input('Enter a score: ')
score = float(score)
if score >= 0.9:
    print('A')
elif score >= 0.8:
    print('B')
elif score >= 0.7:
    print('C')
elif score >= 0.6:
    print("D")
elif score < 0.6:
    print('F')
1.1 will print 'A'
-100 will print 'F'

To check if a value is in a specified range, you can use multiple comparison, where the value is in the middle.

minimum < value < maximum
if minimum is smaller than value and value is smaller than maximum, the result is True

minimum <= value <= maximum
if minimum is smaller or equal than value and value is smaller or equal than maximum, the result is True

You can also negate the result:
not minimum < value < maximum


def get_score(score: float) -> str:
    """
    Function which returns the str A-F which fits to score.
    If the score is not between 0 and 1 (inclusive),
    a ValueError is raised.
    """
    if not 0 <= score <= 1:
        raise ValueError('Bad score')
    if score > 0.9:
        return 'A'
    elif score >= 0.8:
        return 'B'
    elif score >= 0.7:
        return 'C'
    elif score >= 0.6:
        return 'D'
    elif score < 0.6:
        return 'F'
If you use this function get_score, you have to catch the ValueError:

# code

try:
    x = get_score(1.1)
except ValueError as e:
    print(e)
    # reminder,
    # x does not exist
else:
    # success, no exception, x exists
    # do some stuff
    ...
finally:
    # always executed, not needed here
    ...
# code
This will catch all ValueError exceptions and assign it to the name (variable) e.


You could trim the input value, but your comparison allows also values out of range.
But this functionality is needed often.

You can do this as follows:

Minimum of upper_limit and value -> upper_result
Maximum of lower_limit and upper_result -> trimed value


Expressed in code:

def trim(value, lower_limit, upper_limit):
    upper_result = min(upper_limit, value)
    lower_result = max(lower_limit, upper_result)
    return lower_result
After combination it's a bit shorter, but more difficult to understand:

def trim(value, lower_limit, upper_limit):
    return max(lower_limit, min(upper_limit, value))
You could use the functions instead of doing everything on module level.
The benefit is, that you have to do it once right and not 100 times.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
Homework on conditional execution - by Philippa - Jan-03-2020, 01:08 PM
RE: Homework on conditional execution - by DeaD_EyE - Jan-03-2020, 03:05 PM
RE: Homework on conditional execution - by Philippa - Jan-08-2020, 08:33 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020