Problem when denying receipt of a certain value

Asked

Viewed 123 times

-1

I need to create a code that receives 2 notes and me average them and I have to necessarily make my program only receive values from 0.0 to 10.0 what is not happening:

n1 = float(input('Dígite sua primeira nota:'))
n2 = float(input('Dígite sua segunda nota:'))

if n1 > 10.0 and n2 > 10.0:
    print('Nota inválida')
elif n1 < 0.0 and n2 < 0.0:
    print('Nota inválida')
else:
    m = (n1+n2)/2
    print(f'Sua média é {m}')

3 answers

4

You are not knowing how to use logical operators. The and is when you want all the conditions together to be true, and the or is when you want at least one of them to be true. Then it is sufficient that one of the circumstances is of an invalid note that you must enter the if. If you need only one of them to be true you should use one or and not a and. What’s more, if you have two ifThe ones who do the same thing, you have a or implied there, which should already be known since the or indicates that either one or the other is true.

I won’t change all your logic:

n1 = float(input('Dígite sua primeira nota:'))
n2 = float(input('Dígite sua segunda nota:'))
if n1 > 10.0 or n2 > 10.0 or n1 < 0.0 or n2 < 0.0:
    print('Nota inválida')
else:
    print(f'Sua média é {(n1  +n2) / 2}')

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

0


Just to be clear, the statement of the reply of Maniero, already answers the question. However, there are several ways to calculate the arithmetic mean between two numbers.

If you’re using Python 3.8 or higher, you can assemble the following code:

soma = 0
for i in range(1, 3):
    while (n := float(input(f'Digite sua {i}º nota: '))) < 0.0 or n > 10.0:
        print('Valor INVÁLIDO!')
    soma += n
media = soma / 2

print(f'{media = :.1f}')

How this code works?

Initially, we initialize the sum variable. Therefore, it will be responsible for accumulating the sum of the values captured by input().

We later implemented a loop loop for, from which the range(1, 3).

OBS: As we want to capture two values, we must implement the range(1, 3) and not range(1, 2).

Then using concepts of Assignment Expressions, addressed and duly exhausted in the PEP 572, we can implement the while block. This, will be responsible for checking whether the expression assigned to the variable n is minor that 0.0 or greater that 10.0.

Note that while the expression assigned to the variable n is less than 0.0 or more than 10.0, we will receive the message Valor INVÁLIDO! and we will be asked again for the value of that note.

For each valid value entered in input() your sum will be accumulated.

After we have the sum of the two inserted values, the arithmetic mean of these values will be calculated, the same being displayed by print()

From Python 3.8, added a new specifier for f-string. With this new specification we can assemble the following print():

print(f'{media = :.1f}')

The sign of equal "=" in this print() will expand the text of the expression and then the representation of the evaluated expression.

0

A more practical way to avoid notes outside the ranges 0 - 10, is to use a while that forces the user to type the note within the range.

n1 = float(input('Digite sua primeira nota:'))
while not(n1 >= 0.0 and n1 <= 10.0):
    n1 = float(input('Digite uma nota entre 0 e 10:'))

n2 = float(input('Digite sua segunda nota:'))
while not(n2 >= 0.0 and n2 <= 10.0):
    n2 = float(input('Digite uma nota entre 0 e 10:'))

m = (n1+n2)/2
print(f'Sua media é {m}')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.