Summation with halving even terms

I’m having a hard time getting the correct result to the problem below:

Goal

Learn to use loops together with variables to accumulate a result.

Assignment

Assume that n is assigned a positive integer. Write code using the while loop to assign to total the sum of the first n whole numbers as follows:

If the number is even, halve and add it;
If the number is odd, add it.
For example, if n is 5, total will be 1 + (2/2) + 3 + (4/2) + 5 = 12.

Reminder: Whole numbers are 1, 2, 3, … (and so on).


This is what I have, but it doesn’t give me a correct answer.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#initialize counter
total = 0
#use loop to sum the numbers
while count <= n:
if n % 2 == 0:
total += (n // 2)
total += n
count += 1
</code>
<code>#initialize counter total = 0 #use loop to sum the numbers while count <= n: if n % 2 == 0: total += (n // 2) total += n count += 1 </code>
#initialize counter
total = 0
#use loop to sum the numbers
while count <= n:
    if n % 2 == 0:
        total += (n // 2)
    total += n
    count += 1

New contributor

Dinka96 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

In your original code you accumulate total with 2 values whenever n is even.

Better might be:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>n = 5
total = 0
for i in range(1, n + 1):
total += i if (i % 2) else i // 2
print(total)
</code>
<code>n = 5 total = 0 for i in range(1, n + 1): total += i if (i % 2) else i // 2 print(total) </code>
n = 5
total = 0
for i in range(1, n + 1):
    total += i if (i % 2) else i // 2
print(total)

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>12
</code>
<code>12 </code>
12

  1. Update the total based on whether count is even or odd. Right now total += n always runs.

  2. You need to add the count as n, or reword your variables.

  3. Initialize the count to 1

Here is the update:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>n = 2 # or however you are reading it.
# Initialize counter and total
count = 1
total = 0
# Use loop to sum the numbers
while count <= n:
if count % 2 == 0:
total += (count // 2)
else:
total += count
count += 1
print(total) # 40
</code>
<code>n = 2 # or however you are reading it. # Initialize counter and total count = 1 total = 0 # Use loop to sum the numbers while count <= n: if count % 2 == 0: total += (count // 2) else: total += count count += 1 print(total) # 40 </code>
n = 2  # or however you are reading it.

# Initialize counter and total
count = 1
total = 0

# Use loop to sum the numbers
while count <= n:
    if count % 2 == 0:
        total += (count // 2)
    else:
        total += count
    count += 1

print(total)  # 40

Side note: You could rewrite the whole loop using list comprehension in one line:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>n = 10 # or however you are reading it.
# Sum the numbers using list comprehension
total = sum([count // 2 if count % 2 == 0 else count for count in range(1, n+1)])
print(total) # 40
</code>
<code>n = 10 # or however you are reading it. # Sum the numbers using list comprehension total = sum([count // 2 if count % 2 == 0 else count for count in range(1, n+1)]) print(total) # 40 </code>
n = 10  # or however you are reading it.

# Sum the numbers using list comprehension
total = sum([count // 2 if count % 2 == 0 else count for count in range(1, n+1)])

print(total)  # 40

1

I would use a for loop and a range to avoid manually incrementing:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>total = 0
for i in range(1, n + 1):
total += i if i % 2 else i // 2
</code>
<code>total = 0 for i in range(1, n + 1): total += i if i % 2 else i // 2 </code>
total = 0

for i in range(1, n + 1):
    total += i if i % 2 else i // 2

For this specific case an iterator solution is even more applicable, and will give the understanding of Python power:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>total = sum((i if i % 2 else i // 2) for i in range(1, n + 1))
</code>
<code>total = sum((i if i % 2 else i // 2) for i in range(1, n + 1)) </code>
total = sum((i if i % 2 else i // 2) for i in range(1, n + 1))

New contributor

makukha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

0

If you want to have one liner you can do:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>total = sum((i // 2 if i % 2 == 0 else i) for i in range(1, n + 1))
</code>
<code>total = sum((i // 2 if i % 2 == 0 else i) for i in range(1, n + 1)) </code>
total = sum((i // 2 if i % 2 == 0 else i) for i in range(1, n + 1))

where range(1, n + 1) is used to generate numbers from 1 to n.

(i // 2 if i % 2 == 0 else i) checks if the number is even or odd, so if it is even, which is checked as (i % 2 == 0), we add i // 2 and in case it is odd we just add i. and sum() is used to sum all the items.

The result will return also 12 if n=5.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật