Difference between two codes

Asked

Viewed 181 times

1

Code 1, if variable num is 1 or 2, for example when the conv() is called again and I choose option 3 to exit then wheel the print I hope I have been helpful (: but the program does not close, only the next time I choose option 3.

In code 2 the problem does not occur, everything works correctly, I wondered what would be the difference that makes it occur.

Code 1

def conv():

print("\nEscolha umas das duas opcoes abaixo!\n[1] Metro para Centimetro\n[2] Centimetro para Metro\n[3] Sair")
num = input("")
if num == 1:
    met = input("\nDigite o metro\n")
    result = float(met)*100
    print("\nA resposta e >> " + str(result) + " centimetros\n\n")
    conv()
if num == 2:
    cen = input("\nDigite o centimetro\n")
    result = float(cen)/100
    print("\nA resposta e >> " + str(result) + " metros\n\n")   
    conv()
if num == 3: 
    print("\nEspero ter sido util (:\n\n")
    sair()
else:
    print("\nOpcao invalida\n\n")
    conv()

def sair():
    exit

conv()

Code 2

def conv():

print("\nEscolha umas das duas opcoes abaixo!\n[1] Metro para Centimetro\n[2] Centimetro para Metro\n[3] Sair")
num = input("")
if num == 1:
    met = input("\nDigite o metro\n")
    result = float(met)*100
    print("\nA resposta e >> " + str(result) + " centimetros\n\n")
    conv()
else:   
    if num == 2:
        cen = input("\nDigite o centimetro\n")
        result = float(cen)/100
        print("\nA resposta e >> " + str(result) + " metros\n\n")   
        conv()
    else:
        if num == 3: 
            print("\nEspero ter sido util (:\n\n")
            sair()
        else:
            print("\nOpcao invalida\n\n")
            conv()

def sair():

exit

conv()
  • This code has some problems, just solving it doesn’t mean it will be right. Working is different than being right. To fix it, you’d have to rewrite the code. I don’t even know if I can explain why I would need to have a knowledge that I don’t seem to have yet.

3 answers

3


The code has many errors. The correct thing is not to create this lot of function, is to create a loop. And the if can be used as a single block, prefer elif whenever it makes sense.

The code can be improved. For example the output will be according to a condition of the while, or treat the error that would be generated if you type letters where you expect numbers, but I will not present so many new concepts. At the moment try to learn the while and the elif.

while True:
    num = int(input("Escolha umas das duas opcoes abaixo!\n[1] Metro para Centimetro\n[2] Centimetro para Metro\n[3] Sair\n"))
    if num == 1:
        met = input("\nDigite o metro\n")
        result = float(met) * 100
        print("\nA resposta e >> " + str(result) + " centimetros\n\n")
    elif num == 2:
        cen = input("\nDigite o centimetro\n")
        result = float(cen) / 100
        print("\nA resposta e >> " + str(result) + " metros\n\n")   
    elif num == 3: 
        print("\nEspero ter sido util (:\n\n")
        break
    else:
        print("\nOpcao invalida\n\n")
    

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

  • Cool, thanks for the tips.

0

Basically, the first option spends less processing power, because the compiler goes straight through the conditions without getting into conditional daughters.

0

Hello. Well, the code itself is flawed and has many logic errors, it is not recommended to create functions if the code is small, in general, functions are used when there is a certain code that must be repeated in other locations, so it is more readable and lean, facilitating modifications.

In this case, the function conv() nor the function leaving the program get out of(), however, in applications of the genre can be used a loop of repetition that will execute the commands until the condition is false, so we refer to the loop While.

Below is a small algorithm for solving the problem.

  1. Use a while loop equal to True.
  2. While while for True, enter the user’s choice for meters or centimeters .
  3. Check what was the user’s choice, if 1, then convert to Meters, if 2, then convert to cm, if 3, then exit the program.

Browser other questions tagged

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