What Am I Doing Wrong? - Python
Hey I heard about this cool thing, a program that randomly generates 1's & 0's, binary - sorry to insult your intelligence.., then the binary is converted to ascii and compared to a wordlist, then the output is a sentence of random words outputted by the computer.
I started to program this on a flipping a virtual coin sort of route, heads is 0, tails is 1. random.randrange(1,3). But as a test I just tried to get it working in a different context, and failed…
Can Somebody Please Tell Me What Is Going Wrong! Thanks.
Python:
import random import sys
numberFlips = sys.argv[1] numberOnes = 0 numberTwos = 0 whileCount = 0
while whileCount < numberFlips: randomNum = random.randrange(1,3) if randomNum == 1: numberOnes = numberOnes + 1 elif randomNum == 2: numberTwos = numberTwos + 1 else: print "Sorry, An Error Occured!" whileCount = whileCount + 1
print "There Were '" + numberOnes + "' Zeros Produced" print "There Were '" + numberTwos + "' Ones Produced"
NOTE: IF YOU COULDN'T WORK IT OUT FROM THE CODE, YOU ENTER THE NUMBER OF TIMES TO FLIP THE COIN AS A CMD LINE ARGUMENT.
I'm not familiar with the sys,argv[1] command. I rewrote your code using
markupnumberFlips = int(input('How many flips:'))
and it worked fine for me. I would give that a try. If you're using Python 2.x use raw_input instead of input.
Here is my working code. I cleaned up a few things.
import random
numFlips = int(input('How many flips: '))
numHeads= 0
numTails = 0
for i in range(numFlips):
randomNum = random.randint(0, 1)
if(randomNum == 0):
numHeads += 1
else:
numTails += 1
print('There were', numHeads, 'heads, and', numTails, 'tails.')```
Hope that helps.
Just did some reading on argv. It looks like you need to type cast the argument to an int. This code should work for passing the number as an arg.
import random
import sys
numFlips = int(sys.argv[1])
numHeads= 0
numTails = 0
for i in range(numFlips):
randomNum = random.randint(0, 1)
if(randomNum == 0):
numHeads += 1
else:
numTails += 1
print('There were', numHeads, 'heads, and', numTails, 'tails.')```
Make sure to change the .py file so it is executable(chmod + x). Then call it from its directory (./filename.py n) n being some number.
To answer your question: What you're doing wrong is, in adition to not converting the argument to string, is your use of print. You can't just write 'number' and hope it becomes a string. You must either convert the number using str(number), or use:
print "There Were", numberOnes, " Zeros Produced"
Here's how this would look:
import random
import sys
numberFlips = int(sys.argv[1])
numberOnes = 0
numberTwos = 0
whileCount = 0
while whileCount < numberFlips:
randomNum = random.randrange(1,3)
if randomNum == 1:
numberOnes = numberOnes + 1
elif randomNum == 2:
numberTwos = numberTwos + 1
else:
print "Sorry, An Error Occured!"
whileCount = whileCount + 1
print "There Were", numberOnes, " Zeros Produced"
print "There Were ", numberTwos, " Ones Produced"
If you were wondering about the print function in the post above, it's python3 syntax.