Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Module Result
#1
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..
Gribouillis write Jul-12-2023, 11:16 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
The problem is where you assign the value to 1 (= +1) instead of incrementing the value (+= 1).
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020