Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inputting a number and returning the product of all the even integers between 1 and that number

I'm looking for help on the question:

So far my code has gotten me far enough to return the right answer but also the restultant multiplication along the way, i.e.: (1, 2, 2, 8, 8, 48). Can anyone reshuffle or redo the code so it just outputs the answer only, thanks in advance!

counter=1
product=1

userinput=int (input ("What number: "))

for counter in range (1, userinput):
    if counter%2==0:
        product=int (counter*product)
        counter=counter+1

    else:
        counter=counter+1

    print (product)
like image 855
Daniel Whooley Avatar asked Oct 29 '22 00:10

Daniel Whooley


1 Answers

that's because print is executed on every iteration, you need to execute it only after the loop ends, which means print must have the same indent level as the loop,

counter=1
product=1

userinput=int (input ("What number: "))

for counter in range (1, userinput):
    if counter%2==0:
        product= int(counter*product)


print(product)
like image 87
Sufiyan Ghori Avatar answered Nov 04 '22 15:11

Sufiyan Ghori