0

[Abandoned] the output seems to be counting one more after the while condition. why?

1. sooo, i was just uhh, stealing the assignments and doing those assignments myself for practice(please don't sue me XD) aand I came across a problem which i know it could be fixed easily but i want to know why. https://www.sololearn.com/discuss/2739067/?ref=app 2. is my code readable? (ignore the variable naming) here's the code. https://code.sololearn.com/cQiM2TPf6M9d/?ref=app

29th Mar 2021, 12:07 PM
Rellot's screwdriver
Rellot's screwdriver - avatar
2 Antworten
+ 8
Probably because g starts at 1 instead of 0. As for readability, I would put the closing bracket for the while loop on a new line. The rest looks fine to me.
29th Mar 2021, 12:16 PM
Dennis
Dennis - avatar
+ 3
the problem is (as @Dennis said) because the initial value of *g* is 1, so on the very first iteration of your loop the statement g += a adds another one to the result leading to the undesired result. the fix is to initialise *g* with 0 instead here's the fix https://code.sololearn.com/cA208A1A8A53/?ref=app
29th Mar 2021, 12:25 PM
Arsenic
Arsenic - avatar