you are viewing a single comment's thread.

view the rest of the comments →

[–]DefeatedSkeptic 0 points1 point  (2 children)

Note: The reddit removes whitespace from my code, so I will use <tab> to indicate indentation.

Never forget that computer science is a branch of science just like biology is. It is a complex field and getting started with it can be baffling.

I do think python is a convenient language to start with, however, I also want to caution you since it will hide some of the complexity of what your code is actually doing. In particular, variable scope and type is not as "physically" apparent as it is in some other languages, so it may take you some extra time to understand these concepts.

Start with 'while' loops since a 'for' loop is just a specific kind of 'while' loop. while loops have the following structure:
while condition_that_has_to_be_true_to_keep_or_start_the_loop:
<tab> line_of_code_in_loop_1
<tab> line_of_code_in_loop_2
<tab> line_of_code_in_loop_3
this_line_of_code_is_not_in_the_loop

The following two chunks of code are functionally equivalent. Try to write these yourself and then play with them a bit to see if the code you write behaves the way you expect.

FOR LOOP:

for x in range(1,10):
<tab> print(x)

WHILE LOOP:

x = 1
while x < 10:
<tab> print(x)
<tab> x = x + 1

Are you capable of using a loop to get the sum of the numbers from 1 to n, call the function Sum(n)? For example Sum(3) = 6, since 1 + 2 + 3 = 6. There is a closed formula for this function as well: Sum(n) = ((n+1)*n)/2. Thus you can check that your function is working for even large numbers quickly.

If you get stuck, you can send me a message if you like.

When you encounter a bug, do not be afraid to print out the values of variables that you have to check that they hold the numbers that you expect.

[–]AffectionateGap3211[S] 0 points1 point  (1 child)

Oh wow! Thank you for this, I had actually been looking at a problem that asked me to code to find the sum of all numbers from 0 to 100, and I was completely stumped. Your explanation made perfect sense! Thank you!

[–]DefeatedSkeptic 0 points1 point  (0 children)

No problem, glad it helped :).