Members Online
Total Online: 31 Web Spiders: 15
Guests Online: 29
Members Online: 2
Registered Members: 70220 Newest Member: borsche_1110
|
View Thread
| Author |
What Am I Doing Wrong? - Python |
Chunky1106
Member
Posts: 5
Location:
Joined: 29.10.08 Rank: HBH Guru |
|
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. |
|
| Author |
RE: What Am I Doing Wrong? - Python |
tkearn5000
Member

Posts: 32
Location: US
Joined: 13.04.09 Rank: God |
|
I'm not familiar with the sys,argv[1] command. I rewrote your code using
numberFlips = 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.
#!/usr/bin/python
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.
Edited by tkearn5000 on 08-01-10 12:51 |
|
| Author |
RE: What Am I Doing Wrong? - Python |
tkearn5000
Member

Posts: 32
Location: US
Joined: 13.04.09 Rank: God |
|
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.
#!/usr/bin/python
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.
Edited by tkearn5000 on 08-01-10 14:08 |
|
| Author |
RE: What Am I Doing Wrong? - Python |
stranac
Member
Posts: 124
Location: Croatia
Joined: 15.11.08 Rank: God |
|
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. |
|
|
|
|