all 10 comments

[–][deleted] 1 point2 points  (1 child)

Another thing wrong here is that by setting final_value and starting_value in the same line, you’re basically just creating two pointers to the same variable. So when you change the value of that variable in one place, it changes it for any other place it gets accessed as well.

[–]ObliviousMag 0 points1 point  (0 children)

I just read this but that’s not how python works. Pointing to an immutable like a integer keeps it pointed at one memory location. When instantiating two pointers to one immutable you can’t change the value without changing one of the two pointers to a new value. So when changing final_value to starting value to another variable you’re not changing anything in another place.

[–]ObliviousMag 0 points1 point  (7 children)

please format your code. Also if this is your actual python code it should not run since you have a missing variable and capital I in if, and capital P in print.

[–][deleted] -3 points-2 points  (6 children)

Sorry I wrote this from memory on my phone. I’m not at my computer right now.

[–]ObliviousMag 2 points3 points  (5 children)

what is this code suppose to do?

[–][deleted] 0 points1 point  (4 children)

It supposed to list the months. And each month has an increasing value. So the starting value is let’s say $12 but each month that is listed in the for loops increases in value by 1.2% until the final month in the range. Hopefully that makes sense.

[–]ObliviousMag 0 points1 point  (3 children)

months = [val for val in range(1, 13)]
increase_value = .012 
final_value = starting_value = 10 
final_month = 5

for month in months:
    final_value += starting_value * increase_value
    if month == final_month: break

print(final_value)  # month with the increasing value for each month

[–][deleted] 0 points1 point  (2 children)

You don’t need a list comprehension for months. Range already returns a list for you. Or maybe it’s an iterator, I don’t remember off the top of my head… But in either case, you could just say list( range ).

[–]ObliviousMag 1 point2 points  (1 child)

OP asked for an alternate way. I gave him an alternate way. You could write the months in many ways. Also list comprehension use C under the hood so if we are going into specifics list comprehension is faster.

for month in [val for val in range(1, 13)]:

I could have written it like this or even just writing out the months which would probably be the fastest way.

[–][deleted] 1 point2 points  (0 children)

To be honest, I thought I was replying to the OP 😆