Numerical comparator

Asked

Viewed 83 times

1

print ("Insira dois valores para serem comparados:")



Valor1= float(input("Escreva um número:"))
Valor2= float(input("Escreva outro número:"))


print ("Os números que você inseriu foram esses?","\n", Valor1, "\n", Valor2)

sim = str
não = str

if (str(input())=="não") or (str(input())=="sim"):

if str(input(sim)):
    if (Valor1 > Valor2):
        print ("O primeiro valor é maior que o segundo")

    if (Valor2 > Valor1):
        print ("O segundo valor é maior que o primeiro")

    if (Valor2 == Valor1):
        print ("Os números são iguais")


if str(input(não)):
    print ("Então insira os números corretos")

Why does it keep showing the class of the variable Sim after I answered if I correctly typed the values?

And apparently he asks for the answer more than once, I don’t understand why.

I tried to make the code using while instead of if to begin probation and close the sequences with break, but the same problem happened.

  • That’s why I asked, you didn’t accept the answer, you just voted, I realized you didn’t understand the difference, take a look again at [tour].

1 answer

2


This code has a huge amount of errors. Some cases are not quite errors but it leaves the code not so readable or it is not common to write so:

  • I have no idea what those variables are sim andnão, I only eliminated them since they are of no use
  • I didn’t convert the input() for str() because he’s already a string
  • I just compared this entrance to sim and nothing else because that is the only way you should make the comparisons, if you type anything else you will consider as wrong
  • A loop could be used there but depends on requirement, nothing indicates in the question you have, this may have trying to use by kick
  • I preferred to use variable names in lowercase which is normal, and to keep consistent with others
  • I preferred not to treat the raised exception if a suitable number is not entered, but it is most correct to do this
  • Has if too many there and some of these if will work better if they are elifs
  • Perhaps the biggest problem is to ask for confirmation several times, it does not make the slightest sense, it is because you have put in the code asking to type several times that you are asking several times, if you want only once put only once.
  • I would recommend understanding what each thing in the code does before you use it, putting random things in it that seem to fit because you’ve seen somewhere someone doing something like that isn’t going to make you learn to code. You need to change the way you learn to progress.

Behold:

print("Insira dois valores para serem comparados:")
valor1 = float(input("\nEscreva um número:"))
valor2 = float(input("\nEscreva outro número:"))
print("\nConfirme os números que você inseriu digitando 'sim':\n", valor1, "\n", valor2)
if input() == "sim":
    if valor1 > valor2:
        print("O primeiro valor é maior que o segundo")
    elif valor2 > valor1:
        print("O segundo valor é maior que o primeiro")
    elif valor2 == valor1:
        print("Os números são iguais")
else:
    print("Então insira os números corretos")

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

  • I’m trying to fine tune this sense, I’m very new in programming and I’m taking a lot of stone milk to learn, I’m finding a lot of things confusing, especially when it comes to choosing the problems to solve. Thank you very much for the help and hints with the code :)

  • 1

    The reason I’ve identified that is because you’re trying to learn in an unstructured way, it doesn’t work. I need to build knowledge one step at a time.

  • Yes, but precisely my doubt is not finding a very linear way to progress within the programming, it seems that has so much to be apprehended that by the excess of options, makes everything a little more confusing. Even more than my goal with programming itself, is to make some techniques that I use in physics, chemistry and mathematics something more palatable, but transcribing this to a logic that the computer understands, is being my challenge.

Browser other questions tagged

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