Click here to Skip to main content
15,868,340 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
 
Thank you for all you guys help the solution ended up being.

sum= 0
count= 1
while (count <= 100):
sum+= count
count+= 1
print(sum)
 
Share this answer
 
Comments
CPallini 15-Mar-24 6:36am    
Best solution it is the Gauss one:
print(100*101/2);
0x01AA 16-Mar-24 14:14pm    
Haha, mentioned in the striked out solution: Gaußsche Summenformel – Wikipedia[^]. But strange, I do not find that in an english version ...
PIEBALDconsult 15-Mar-24 10:32am    
Please don't try to answer your own question. You can use the Improve question button to add context and detail.

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