Help with this terrible riddle for a beginner!

Asked

Viewed 63 times

-5

I want to identify odd numbers, but leave with a print formatting with no whitespace

EX:"These are the odd ones: (1, (), 3, '')" I want you to show it like this: "These are the odd ones: 1, 3,"

Follow the code for better understanding:

soma=0
a=int(input('Diga um número:\n'))
a1=int(input(''))
a2=int(input(''))
a3=int(input(''))
if a%2==1:
    soma+=a
    b=a
else:
    soma+=0
    b=()
if a1%2==1:
    soma+=a1
    b1=a1
else:
    soma+=0
    b1=()
if a2%2==1:
    soma+=a2
    b2=a2
else:
    soma+=0
    b2=()
if a3%2==1:
    soma+=a3
    b3=a3
    
else:
    soma+=0
    b3=('')   
print(f'Soma dos ímpares: {soma}')

print(f'Esses são os ímpares:{b,b1,b2,b3}')

1 answer

1


To be honest, the code is very confusing... I will try to pass some tips and demonstrate how I would do. Soon, is with a cool logic, but it is good to evolve some more classes to see the basics (conditional and repetitions).

If you pick 4 variables, you don’t actually need to give 1 if to each one... you should use a repeat loop to make your code easier (you can use FOR if you know how many times you need to repeat and WHILE if you’re not sure).

Another thing you should use is a list to store odd numbers, via the append command.

I will try to put a simple code that you will be able to see several new things (list and loop). In this case you can make the code much easier by leaving it as follows:

impares = []
for numero in range(0, 4):
num = int(input('Digite um numero: '))
if num % 2 == 1:
    impares.append(num)
    print(f'{num} é um número impar')
else:
    print(f'{num} não é um número impar.')
print(impares)

Browser other questions tagged

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