Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Python
count = 0
sum = 1
while (count <= 100):
 count += 1
 print(count)
 if (count > 100):
  print (sum)


What I have tried:

I have tried many different options but nothing seems to be working correctly. I am still a beginner coder, so my coding is pretty basic.
Posted
Updated 14-Mar-24 21:00pm
v3

You did not explain what you excpect and what you get. Anyway...

1. What is the aim of continue_flag? I don't see a reason for that. Remove it.
2. Sum of 1 up to xyz: Ok starting with zero is not critical because adding 0 does not change anything. But for nitpickers like me, starting from 1 is the right way.
3. up to 100
anything 'less than' ('<') 100 does not include 100. Therefore <= 100 is most probably your problem

I hope it helps.

[Edit]
Strange: I do not find a english version of Gaußsche Summenformel – Wikipedia[^]

[Edit 1]
After updating your question, my answer looks to be useless....

[Edit 2]
Simply do something like this
Python
sum= 0;
count= 1;
while (count <= 100) 
  sum+= count;
  count+= 1;

// After the above, sum should have the value you expect
 
Share this answer
 
v9
Comments
Maciej Los 16-Mar-24 16:02pm    
5ed!
0x01AA 17-Mar-24 9:25am    
Thank you very much Maciej
This isn't a complex problem, it's designed so a beginner can get it done fairly easily - and learn how to start coding; building the skills he needs. You need to do this for yourself because the next task will be more complex - it assumes that you understood everything here and have the rudiments of the skills you will need. Just copying and pasting 0x01AA's solution doesn't teach you anything, any more than watching the Tour De France will teach you to ride a bicycle.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Comments
Matt T Heffron 12-Apr-24 19:59pm    
Yes, it's as useless of a problem now as it was when given to young Carl Friedrich Gauss. His solution would, of course, be the "right" way to compute that number.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900