Python Forum
Module Result - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Module Result (/thread-40330.html)



Module Result - EddieG - Jul-12-2023

I have been writing this code which must result in different result after running the code for heads and tails. I am having only one same result each time and not random.

code:
import random
output={"Heads":0, "Tails":0}
coin=list(output.keys())

for i in range(10000):
    output[random.choice(coin)]=+1

print("Heads:", output["Heads"])
print("Tails:", output["Tails"])
Result in IDLE Shell 3.11.4
Output:
Python 3.11.4 (tags/v3.11.4:d2340ef, Jun 7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. = RESTART: C:/Users/hp/Desktop/Pythoncodes/rdmhead.tails.py Heads: 1 Tails: 1 =========== RESTART: C:/Users/hp/Desktop/Pythoncodes/rdmhead.tails.py ========== Heads: 1 Tails: 1 =========== RESTART: C:/Users/hp/Desktop/Pythoncodes/rdmhead.tails.py ========== Heads: 1 Tails: 1 Expected Results: =========== RESTART: C:/Users/hp/Desktop/Pythoncodes/rdmhead.tails.py ========== Heads:4993 Tails:5007 =========== RESTART: C:/Users/hp/Desktop/Pythoncodes/rdmhead.tails.py ========== Heads:5080 Tails:4920 =========== RESTART: C:/Users/hp/Desktop/Pythoncodes/rdmhead.tails.py ========== Heads:4912 Tails:5061
Is there some revision on the code required to get the same result as the textbook example?
Newbie here..


RE: Module Result - deanhystad - Jul-12-2023

The problem is where you assign the value to 1 (= +1) instead of incrementing the value (+= 1).