Can you do it with less variables?

Asked

Viewed 101 times

0

Constructing a program prompts the user to type positive integers, ending with 0, whose output is "yes X", without the quotation marks, if the sequence contains X positive values and forms an increasing sequence; otherwise print "not X".

a = int(input("Digite o primeiro valor da sequencia: "))
b = int(input("Digite o segundo valor da sequencia: "))

i=1

if a == 0:
    print("nao ", 0)
if a>0 and b == 0:
    print("sim ", 1)

while a>0 and b>0:
    i=2
    c = int(input("Digite outro valor da sequencia: "))
    if c>b:
        i = i + 1
    if c<b:
        i = i + 1
    a = int(input("Digite algum outro valor da sequencia: "))
    b = a
    c = b
    a = c

if a == 0 and c>b:
    print("sim ", i)
if a == 0 and c<b:
    print("nao ", i)
if c == 0 and c>b:
    print("sim ", i)
if c==0 and b<c:
    print("nao ", i)
  • Apparently not, but you can do better, but the code is confused and we don’t know the goal, which makes it difficult to say something appropriate.

  • So the goal is "a program that prompts the user to enter positive integers, ending with 0, and whose output is "yes X", without the quotation marks, if the sequence contains X natural values and forms a crescent sequence; otherwise print "not X".

2 answers

0

Hello, I don’t know if you’ve learned about lists in python but will a very simple code And I don’t understand why you want me to print 'yes, X' and 'no, X' but I did. attribute to the variable sequence the '[]' so that it will be a list, and I made a simple loop whenever it is True it will continue if the a is equal to '0' it gives a break in the loop otherwise it adds to the list with the method . append() the variable a. At the end it prints the list. Sorted(X) == sort X(a list) crescently. I hope I’ve helped.

sequencia = []
x = 0
while True:
    a = int(input('Digite o valor'))
    if a == 0:
        print('não', x)
        break
    else:
        print('sim', x)
        sequencia.append(a)
    x += 1




print(sorted(sequencia))

0

To read the sequence, just modify a little the answer I published in:

Accept only numerics in input

numeros = []

while True:
    try:
        numero = int(input("Informe um número inteiro positivo: "))
        if numero < 0:
            raise ValueError("O número deve ser inteiro positivo ou zero.")
        elif numero == 0:
            break
        else:
            numeros.append(numero)
    except ValueError as e:
        print("Valor inválido:", e)

And, to check if the list of numbers is in ascending order:

ordenado = numeros == sorted(numeros)

Finally, to get the list size, the native function len:

tamanho = len(numeros)

Thus staying:

numeros = []

while True:
    try:
        numero = int(input("Informe um número inteiro positivo: "))
        if numero < 0:
            raise ValueError("O número deve ser inteiro positivo ou zero.")
        elif numero == 0:
            break
        else:
            numeros.append(numero)
    except ValueError as e:
        print("Valor inválido:", e)

ordenado = numeros == sorted(numeros)
tamanho = len(numeros)

print(f'sim {tamanho}' if ordenado else f'não {tamanho}')

See working on Repl.it

Browser other questions tagged

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