Please go easy, I am a student!
I am unable to export the results of the following code into a CSV. For each loop (coin flip), I need to export the result to a CSV called RESULTS.CSV as a new ROW.
I have tried about four different approaches posted on StackOverflow but been unsuccessful thus far. I am having a difficult time understanding the syntax/logic. I hate asking this and always try to avoid it, but would anyone mind showing me a simple solution so that I can dissect the logic? (rather than referencing to an article, as I have already tried to understand them but am having trouble)
Version: Python 3.5
import random
flipcount=int(input("How many times should I flip a coin?\n###:"))
samples = [ random.randint(1, 2) for i in range(flipcount) ]
heads = samples.count(1)
tails = samples.count(2)
for s in samples:
msg = 'Heads' if s==1 else 'Tails'
print(msg)
print ("Heads count=%d, Tails count=%d" % (heads, tails))
Here is a failed attempt... I believe that I have saturated my research on the topic and have greatly confused myself...
import random
import csv
flipcount=int(input("How many times should I flip a coin?\n###:"))
samples = [ random.randint(1, 2) for i in range(flipcount) ]
heads = samples.count(1)
tails = samples.count(2)
writer = csv.writer(w)
for s in samples:
msg = 'Heads' if s==1 else 'Tails'
print(msg)
with open('some.csv', 'w', newline='') as f:
writer.writerows(msg)
print ("Heads count=%d, Tails count=%d" % (heads, tails))