jeudi 27 février 2020

Make this code shorter without using loops

import random
a = random.randrange(1,100)
b = random.randrange(1,100)
c = random.randrange(1,100)
d = random.randrange(1,100)
e = random.randrange(1,100)
print('Random numbers: ',a,b,c,d,e)
sumEven = 0
sumOdd = 0
print("Even numbers: ")
if (a%2)==0:
    sumEven = sumEven+a
    print(a,end=" ")
if (b%2)==0:
    sumEven = sumEven+b
    print(b,end=" ")
if (c%2)==0:
    sumEven = sumEven+c
    print(c,end=" ")
if (d%2)==0:
    sumEven = sumEven+d
    print(d,end=" ")
if (e%2)==0:
    sumEven = sumEven+e
    print(e,end=" ")
print("\nOdd numbers: ")
if (a%2)!=0:
    sumOdd = sumOdd+a
    print(a,end=" ")
if (b%2)!=0:
    sumOdd = sumOdd+b
    print(b,end=" ")
if (c%2)!=0:
    sumOdd = sumOdd+c
    print(c,end=" ")
if (d%2)!=0:
    sumOdd = sumOdd+d
    print(d,end=" ")
if (e%2)!=0:
    sumOdd = sumOdd+e
    print(e,end=" ")

    print("\nSum of all even numbers: ",sumEven)
    print("Sum of all odd numbers:", sumOdd) 

Please make it shorter suggest shorter code, but without using loops the purpose of the program is to separate odd numbers from even numbers and get the sum of all odd numbers also get the sum of all even numbers

Aucun commentaire:

Enregistrer un commentaire