Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

(READ COMMENTS) Python temporarily changes my variable, how do i stop that?

So i have this code:

from random import randint
for i in range(9999999):
    a=input("press any key to roll the wheel: ")
    money=0
    money2=randint(0, 400)
    money+=money2
    print(money)

And while i was testing the program, i found that it isn’t doing what i expected it to do (for example: add 382 with 196), i wanna ask, whats wrong? Everything looks to be working just fine, just that it seems to change the "money" variable temporarily, after that it resets. What happens when you press enter multiple times

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You keep setting money to 0 in the loop. Do this:

from random import randint
money=0
while True:
    a=input("press any key to roll the wheel: ")
    money2=randint(0, 400)
    money+=money2
    print(money)

It’s better to use while True as well.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading