0

in that code the operation dont work well, and I know that if I convert a str to a int it will work but the question is: why in the first code works well and the second not ?

first code python 3:

disciplina = input('digite suas disciplina ')
nota_final = input ('digite sua nota final (entre 0 e 100)')

 if disciplina =='Historia' and nota_final >= '70' :
    print('Você foi aprovado !')
else:
    print('Você precisa estudar mais ')

seconde code python 3 :

aluno = input('Você é um bom aluno ?')
nota = input ('Quanto você costuma tirar na prova ?')

 if aluno =='sim' and nota >= '10' :
   
   print( 'Parabéns você é demais !')
else:
   print('Você precisa estudar mais')

obs: In the fisrt code works well if you type a number less than 70 and in the 2nd code does not works well when you type a number less than 10 like it must be , why ? before you say "you need to convert the 2nd code nota to int(input ( "texto"), and then i ask you why the first code does not need it (all is in string in nota_final= input("text")) ???

Chris
  • 15,819
  • 3
  • 24
  • 37
  • 5
    You are comparing strings not integers, so the behavior is going to vary considerably from what you expect. You need to cast it as int, and you need to remove the quotes from your ints in your conditional statement. – Chris Sep 21 '21 at 17:01
  • 5
    You are comparing strings, not numbers. `7 < 10`, but `'7' > '10'`, because only the first characters of each string need to be compared, and `'7'` comes after `'1'` lexicographically. – chepner Sep 21 '21 at 17:01
  • 3
    Your first code only *appears* to work, because you haven't tried it with `100` yet, or `8` or `9`. – chepner Sep 21 '21 at 17:03
  • 2
    What do you think the "bug" in Python is here? You can compare strings to strings, this is well documented, and not surprising behavior at all. They are compared lexicographically, which is why the first *sorta* appears to work with numbers "less than" 70. Definitely not a bug. – juanpa.arrivillaga Sep 21 '21 at 17:03
  • 1
    As explained by the others, you should compare integers, not strings. int(nota) >= 70 or int(nota) >= 10 – bkakilli Sep 21 '21 at 17:03
  • even better imo to cast to int when you define the variable: `nota = int(input(...))` – Robin Zigmond Sep 21 '21 at 17:05
  • @chepner or `'8'` and `'9'`. – juanpa.arrivillaga Sep 21 '21 at 17:05
  • "before you say "you need to convert the 2nd code nota to int(input ( "texto"), and then i ask you why the first code does not need it (all is in string in nota_final= input("text")) ???" **both need to use `int` objects to work correctly** In the first code, it does not "work well". – juanpa.arrivillaga Sep 21 '21 at 17:08
  • @juanpa.arrivillaga, this is not well documented because it is extremely basic, and it could be surprising for a newbie. Also, we can expect a newbie to not know how to dig out (or existence of) documentation, right? :) – bkakilli Sep 21 '21 at 17:09
  • @bkakilli it is well documented, in the section on [comparison operators in the docs](https://docs.python.org/3/reference/expressions.html#value-comparisons). "Strings (instances of `str`) compare lexicographically using the numerical Unicode code points (the result of the built-in function `ord()`) of their characters." As far as what we can expect people to do, I say you can start learning to use documentation *pretty much immediately*. This is a good opportunity. – juanpa.arrivillaga Sep 21 '21 at 17:12

1 Answers1

0

Comparisons between strings works from left to right, character by character, so string '9' is greater than '10' because the comparison starts with '9' being greater than '1'.

In order to have Python process the numbers as actual numbers, you need to either convert them to a numeric data type (e.g. int(nota_final) > 70) or right justify the strings (e.g. nota_final.rjust(3) > ' 70'). Note that converting to a numeric type will have fewer edge cases to manage

Alain T.
  • 40,517
  • 4
  • 31
  • 51