Read two notes and display a string according to the average between them

Asked

Viewed 188 times

-2

Make a program that receives two notes, calculate and show the average and the message that is in the following table:

0,0 - 4,0   Reprovado
4,0 - 7,0   Exame
7,0 - 10,0  Aprovado
print('ATIVIDADE 2 -ESTRUTURA CONDICIONAL')
print('----------------------------------')

nota_1 = float(input('Digite a primeira nota: '))
nota_2 = float(input('Digite a segunda nota: '))

média = (nota_1+nota_2)/2

print('A média aritmética é: ',média)

if float(média <=4.0):
    print('Reprovado')

elif float(média <=7.0):
    print('Exame')

elif float(média <=10.0):
    print('Aprovado')
  • 2

    What is your doubt?

  • how do I enter those table values into the program using conditional

  • 1

    Hello @Halan, welcome to Sopt, You need to let us know what your question is, take a look at our [Tour] and [Ask]. - To add information click on [Edit] below the question. = P --- In your code there are 2 things I wouldn’t do, Accentuated variable and convert the boolean in the if for float.

  • Hi, I believe that is accent even, that can not be used, because of the conversion of the compiler itself.

  • 1

    @Philipramkeerat Yes, Python accepts Unicode names without any problem.

  • @Andersoncarloswoss I remembered the utf-8 that we can insert

  • Instead of if float(média <=4.0):, you should use if float(média) <= 4.0:, that is, the parenthesis is in the wrong place. Better still would simply use if média <= 4.0: as given in the answers. The same applies to elif. Therefore, I consider your question to be a typo.

Show 2 more comments

3 answers

1

I would risk something like:

media = (nota_1 + nota_2)/2
if media <= 4.0:
    print('reprovado')
elif media >= 7.0:
    print('aprovado')
else:
    print('exame')

Now, it’s not enough to know HOW, but yes WHAT this doing, first of all, avoid using accentuation as this can generate many problems by file formatting compatibility, this becomes even more dangerous in interpreted languages (as the case of Python).

Understanding the code: well, I did not put integer, but only the part that you doubt, note that I receive the values of the notes and do the average calculation, Python understands that the variable media is a float because she gets float as a value.

Understanding the conditional ones if, elif and else: they work with value boolean, that is to say, true or phony, do not compare different values from this, note that in media <= 4.0 the result of this comparison is a booleanalthough the data are float, That’s because the parole is comparing the condition <=, that is to say: "media is greater than or equal to 4.0, true or false?".

Notice that I set up a different structure, because understanding the problem of the table I see that the average being less than or equal to 4.0, the student is failed, if it is between 4.1 and 6.9 he enters the exam, but if it is above 7.0 he is approved.

  • 1

    "avoid using accentuation", is not true, as Python supports Unicode names without any problems.

  • @Andersoncarloswoss, the fact that Python supports (not the only language) does not diminish the fact that not using is a good practice, the use of accentuation is strongly discouraged.

  • For what reasons?

  • @Andersoncarloswoss, follows internal references: - https://answall.com/questions/83725/%C3%89-a-good-idea-declare-Vari%C3%A1veis-with-accents -. https://answall.com/questions/16555/existe-algum-problema-em-usar-caracteres-unicode-para-identificadores-em-c%C3%B3digo? noredirect=1&lq=1 Here it is possible to see other references also that end up helping to understand why it is not a good practice despite being allowed. Remembering, some languages allow the declaration of emojis, think about the readability and lack of pattern of this and already have a good reason not to use.

1

Your code produces the expected output, as can be seen here: https://sopt-question-381571.acwoss.repl.run.

But there are considerations that will be important:

  1. The initializing method of float, called on float(input('...')), throws an exception ValueError if the value returned by input cannot be represented as a floating point. This happens, for example, when a text is returned. It’s interesting you treat this in the code to give the feedback correct to the user and not break the application:

    while True:
        try:
            nota_1 = float(input('Entre com a nota 1:'))
            break
        except ValueError:
            print('Valor inválido! A nota precisa ser um número real')
    
  2. There is no reason to consider negative marks in your code - because it would be strange for a student to get -5 on the test. Considering that the maximum note of the statement is 10.0, also has no reason to consider notes above that, so validating the range is also important:

    while True:
        try:
            nota_1 = float(input('Entre com a nota 1:'))
            if not 0 <= nota_1 <= 10:
                raise ValueError('Valor fora do intervalo')
            break
        except ValueError:
            print('Valor inválido! A nota precisa ser um número real entre 0 e 10.')
    
  3. In the checks to display the result you made float(média <= 4). Notice that you put the entire comparison inside float. This works, but by mere "coincidence" as a consequence of the operator <= return a boolean and the boolean in Python is a subtype of the integer. So, if returned False, will be considered as 0.0, which will be interpreted as false by the structure if, similarly when returned True, will be considered 1.0, which will be interpreted as true. It would make more sense (read item 4) to do:

    if float(média) <= 4.0:
        ...
    elif float(média) <= 7.0:
        ...
    else:
        ...
    
  4. The operator /, that will make the arithmetic division between the sum of the notes and the quantity, to calculate the average, already returns by default a floating point number. That is, to do média = (nota_1 + nota_2)/2 already makes it média be a float, then do float(média) is redundant.

  5. You can generate strings repetitive through multiplication, with the operator *. For example, do '-' * 3 will generate the string '---'. This way, you don’t need to type all the characters like you did; and allied to this you can make the code more dynamic if you need to change the information:

    title = 'ATIVIDADE 2 -ESTRUTURA CONDICIONAL'
    
    print(title)
    print('-' * len(title))
    

0

Just to complement the answers, Serves for version 2 of python. following a different path if you are in need of using the accent in your work, course, college etc. You can use a syntax that allows the use by configuring the UTF-8 at the beginning of your code.

For example:

-*- coding: utf-8 -*-

According to python documentation, Unicode ( https://www.unicode.org/ ) is a specification that aims to list all characters used by human languages and give each character a unique code. Unicode specifications are continuously revised and updated to add new languages and symbols.

  • And that’s not only necessary for Python 2?

  • @Andersoncarloswoss I was working with python 3.7 and I took the test to be able to post here, I believe it serves for the 3 tbem

  • Really, in version 3 does not need, I edited the post, to inform that it is in version 2 now, thanks @Andersoncarloswoss xD

Browser other questions tagged

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