Difference between two codes

1

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

In code 2 the problem does not occur, everything works correctly, I wanted to know 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()
    
asked by anonymous 13.03.2017 / 18:34

3 answers

3

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

The code can be improved. For example the output is according to a condition of while , or treat the error that would be generated if you enter letters where numbers are expected, but I will not introduce as many new concepts. At the moment try to learn while and 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")

See working on ideone (it's off the air now). And No Coding Ground . Also put it on GitHub for future reference .

    
13.03.2017 / 18:58
0

Basically, the first option spends less processing power because the compiler goes through the conditions without going into daughter conditionals.

    
13.03.2017 / 18:43
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 the same becomes more legible and lean, making modifications easier.

In this case, the function conv () and the function that exits from the exit () program, however, in a similar application a loop of repetition can be used. will execute the commands until the condition is false, so we refer to the While loop.

Below is a small algorithm for solving the problem.

  • Use a while loop equal to True.
  • While while for True, enter the user's choice for meters or centimeters.
  • Check the user's choice, if 1, then convert to Meters, if 2, then convert to centimeters, if 3, then exit.
  • 15.03.2017 / 15:38