Python Forum
Homework on conditional execution
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Homework on conditional execution
#1
Please how do i make my computation stay in the range between 0.0 and 1.0
score = input('Enter a score: ')
if float(score) > 1.0
    print('Bad score')
if float(score) >= 0.9:
    print('A')
elif float(score) >= 0.8:
    print('B')
elif float(score) >= 0.7:
    print('C')
elif float(score) >= 0.6:
    print("D")
elif float(score) < 0.6:
    print('F')
elif float(score) < 0.0
    print('bad score')
else:
    print('Bad score')
Reply
#2
You cannot work with range thats does not support float numbers.
You can rewrite this with something like this:

score = float(input('Enter a score: '))
if (score*10) in range(0, 10, 1):
    if float(score) > 10:
        print('Bad score')
    if float(score) >= 9:
        print('A')
    elif float(score) >= 8:
        print('B')
    elif float(score) >= 7:
        print('C')
    elif float(score) >= 6:
        print("D")
    elif float(score) < 6:
        print('F')
    elif float(score) < 0:
        print('bad score')
    else:
        print('Bad score')
If you still want to use 0.0-1.0 you can search for "NumPy"
Reply
#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
#4
There's a few problems here. First, you need a colon after lines 2 and 14. Now, if you enter something greater than 1, two conditionals trigger: the ones on line 2 and line 4. To stop this, make line 4 an elif, so that it only triggers if the number is 1.0 or less (that is, only if line 2 doesn't trigger). If you enter a number less than 0, line 12 triggers, when you really want line 14 or 16 to trigger. But look at line 12: you are testing if the number is less than 0.6. But you already know it's less than 0.6, because you've been testing greater than or equals for all the numbers 0.6 and above. So line 12 could just test that the number is greater than 0, and score that as an F. Then you could get rid of lines 14 and 15, and it should all work.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Simplify.
Float just once. You can add the Value Error trap as mentioned above around that.
Then, invert. Go up instead of down. As written, a score of 0.95 will give a printout of A through F.
score = float(input('Enter a score 0-1: '))
grade = "Bad score"
if score >= 0:
  grade = 'F'
if score >= 0.6:
  grade = 'D'
if score >= 0.7:
  grade = 'C'
if score >= 0.8:
  grade = 'B'
if score >= 0.9:
  grade = 'A'
if score > 1.0:
  grade = "Bad score"

print(grade)
Reply
#6
Thank you so much jefsummers. I understand it better now.

Thank you everyone, you were all so helpful
Reply
#7
This is homework about conditional execution, therefore I suggest to use ascending comparison style. Arguably it makes reading a code easier:

# instead of 'if zero less or equal than score'

if score >=0:

# use 'if score greater or equal to zero'

if 0 <= score 
Sweet memories: once upon a time my teacher described descending comparison style with giving directions this way: 'go straight and in 15 minutes you reach the square. You have to turn left five minutes before you reach the square.'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

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