Rauno Ehasalu
Rauno Ehasalu

Reputation: 25

loop issue with automatic gifts

I need to make a program for school that calculates given gifts.

The program should look as work as follows:

Program asks how many buyers, and every every second buyer will get a free gift as - first buyer will get one gift, second will get nothing, third will get three gifts and so on

For example, if user enters 8 buyers, then the sum would be 16 because 1 + 3 + 5 + 7 = 16

The below version worked but....

buyers = int(input("Input how many buyers: "))
i = 1
while i < buyers:
    i = i * 4
print("Free gifts " + str(i))

If user entered 2 then the program should give 1 but this one gave 2. If user entered 3, 7 or 8 then the answer was correct. But if user entered for example 100 then I expected a answer 2500 but program gave me 256.

I would appriciate if someone would help me out with this one.

Upvotes: 1

Views: 86

Answers (3)

Cohan
Cohan

Reputation: 4564

If there is a constraint where you are required to use a while loop, I assume you're taking an intro course to python and aren't exposed to list comprehensions yet. While this is far from a pythonic approach, this is probably within the constructs of where you are in your course.

The key (unless you can do it in a one liner) is that you will need to have a separate variable to track the gifts independently of the buyers. It's also helpful to identify the pattern of how free gifts are distributed, which is simply the sum of all odd numbers below the number of buyers.

buyers = int(input("Input how many buyers: "))

gifts = 0

i = 0
while i < buyers:

    if i % 2 == 1:
        gifts += i

    i += 1

print("Free gifts " + str(gifts))

If you were to put it in a function, it would be easy to quickly test.

def how_many_gifts(buyers):

    gifts = 0

    i = 0
    while i < buyers:

        if i % 2 == 1:
            gifts += i

        i += 1

    return gifts

Test a range of inputs.

outstr = "{} buyers = {} free gifts"

for i in range(11):
    print(outstr.format(i, how_many_gifts(i)))

print(outstr.format(100, how_many_gifts(100)))

and get the following output

0 buyers = 0 free gifts
1 buyers = 0 free gifts
2 buyers = 1 free gifts
3 buyers = 1 free gifts
4 buyers = 4 free gifts
5 buyers = 4 free gifts
6 buyers = 9 free gifts
7 buyers = 9 free gifts
8 buyers = 16 free gifts
9 buyers = 16 free gifts
10 buyers = 25 free gifts
100 buyers = 2500 free gifts

The one liner approach would be

gifts = sum(range(1, buyers+1, 2))

This kind of brevity is why python can be a real pleasure to work in.

Upvotes: 4

Prune
Prune

Reputation: 77860

Your posted program finds the largest power of 4 that's <= the quantity of buyers; I'm not sure how that is supposed to relate to the problem given.

Your assignment, in mere arithmetic terms, is to add up all the odd numbers <= the quantity of buyers:

# Get the input as you're already doing

i = 1
gifts = 0

while i <= buyers:
    gifts += i
    i += 2

# Print the output as you're already doing

As a one-liner:

gifts = sum(range(1, buyers+1, 2))

Upvotes: 0

vencaslac
vencaslac

Reputation: 2884

use this:

buyers = int(input("Input how many buyers: "))
gifts = sum([item for item in list(range(buyers+1))[1::2]])

print("Free gifts " + str(gifts))

it's the sum of all numbers up to n+1 with a stride of 2

Edit - with while:

buyers = int(input('Input how many buyers'))
to_sum = [item for item in list(range(buyers+1))[1::2]]
gifts=0
while len(to_sum) != 0:
    gifts+=to_sum[0]
    to_sum.pop(0)

print('Free gifts: ' + str(gifts))

Upvotes: 2

Related Questions