Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

What Am I Doing Wrong? - Python


Chunky1106's Avatar
Member
0 0

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.


tkearn5000's Avatar
Member
0 0

I'm not familiar with the sys,argv[1] command. I rewrote your code using

markupnumberFlips = int(input(&#39;How many flips:&#39;))

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(&#39;How many flips: &#39;))
numHeads= 0
numTails = 0

for i in range(numFlips):
    randomNum = random.randint(0, 1)
    if(randomNum == 0):
        numHeads += 1
    else:
        numTails += 1

print(&#39;There were&#39;, numHeads, &#39;heads, and&#39;, numTails, &#39;tails.&#39;)```

Hope that helps.

tkearn5000's Avatar
Member
0 0

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(&#39;There were&#39;, numHeads, &#39;heads, and&#39;, numTails, &#39;tails.&#39;)```

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.

stranac's Avatar
Member
0 0

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 &lt; numberFlips:
	randomNum = random.randrange(1,3)
	if randomNum == 1:
		numberOnes = numberOnes + 1
	elif randomNum == 2:
		numberTwos = numberTwos + 1
	else:
		print &quot;Sorry, An Error Occured!&quot;
	whileCount = whileCount + 1

print &quot;There Were&quot;, numberOnes, &quot; Zeros Produced&quot;
print &quot;There Were &quot;, numberTwos, &quot; Ones Produced&quot;

If you were wondering about the print function in the post above, it's python3 syntax.