Success Juggler in Python

16

I have to create a program that asks the user for natural numbers or 0 to finish and for each number that the user enters, the program must create a juggling sequence and display them on the screen.

In turn, when the program ends, you must show on the screen which was the number that generated the longest and shortest succession.

This is what the succession does:

  

if number is even = > number = number ^ (1/2)
  if number is odd = > number = number ^ (3/2)

And the sequence ends when number = 1

Here the code:

numero = int(input('Ingrese un numero natural o 0 para terminar: '))
contador_max = 0
contador_min = 1

while numero != 0:
    num = numero
    contador = 0

    while num != 1:
        if num % 2 == 0 :
            num = int(num ** 0.5)
            print (num,end=' ')    
        else :
            num =int(num ** 1.5)
            print(num,end=' ')
        contador =+ 1

    if contador > contador_max:
        contador_max = contador
        maximo = numero
    if contador < contador_min:
        contador_min = contador
        minimo = numero
    print()
    numero = int(input('Ingrese un numero natural o 0 para terminar: '))
print('La sucesion mas larga se genero con el numero:',maximo)
print('La sucesion mas corta se genero con el numero:',minimo)

All this I must do only using While, if, elif, else and some other that appear there in the code.

As you can see, my problem occurs when I finish the program, since it tells me that the variable minimo is not defined, and before that error, the other was that, when I said which number I generated the longest and the shorter sequence, I showed the same number for both (the maximum).

    
asked by Dark.R 29.11.2018 в 15:43
source

1 answer

8

There are a couple of errors in your code.

  • The value of contador_min should be initialized to an arbitrarily large number. As you have it, initialized with 1, it will never change that value since any sequence of "juggling" will be longer than 1. That's why the variable minimo is never assigned and it gives you that error. To fix it do contador_min=1000000 for example.
  • Almost more important (and harder to see!) to increase the counter, instead of contador += 1 you have set contador =+ 1 , which is basically the same as contador = +1 . That is, instead of increasing the counter, you always assign the value 1!

Correcting both errors the program is already working correctly.

    
answered by 29.11.2018 / 16:58
source