+ 2

Please explain (Simple Python)

I want this number to increase by two every loop, but it does not seem to be working. What is the issue? https://code.sololearn.com/cI1ctM5DFEPa/?ref=app

20th Oct 2020, 6:56 AM
Cam UOR
Cam UOR - avatar
4 Answers
+ 6
Your first issue is that you have your operator incorrect for Miles_of_Road=+ 2 Now you have a couple of ways you can achieve what you wish to do. If you want to change the Miles_of_Road global variable directly from within the work() function then you'll need to make a 'global' declaration in the function prior to using the variable. You'll also no longer need the key variable. So your code will look something like; def work(): global Miles_of_Road Miles_of_Road += 2 print(f"There are now {Miles_of_Road} miles of road") for ww in time: work() Otherwise, if you don't wish to use the global keyword and pass the value you can do something like; def work(key): key += 2 print(f"There are now {key} miles of road") return key for ww in time: Miles_of_Road = work(Miles_of_Road)
20th Oct 2020, 7:12 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
import random Miles_of_Road =0 print(random.randint(1, 10)) print(Miles_of_Road) time= list(range(5)) print(time) def work(key): print(f"There are now {key} miles of road") return key for ww in time: work(Miles_of_Road) Miles_of_Road += 2 print(Miles_of_Road)
20th Oct 2020, 7:13 AM
suraj mehra
suraj mehra - avatar
+ 2
Try this
20th Oct 2020, 7:14 AM
suraj mehra
suraj mehra - avatar
+ 2
ChaoticDawg Thank you
20th Oct 2020, 9:04 AM
Cam UOR
Cam UOR - avatar