Join us at IRC!
Hacking isn't just Computers & Exploits. It's a Philosophy. - Mr_Cheese
Friday, May 25, 2012
Navigation
Members Online
Total Online: 31
Web Spiders: 15
Guests Online: 29
Members Online: 2

Registered Members: 70220
Newest Member: borsche_1110
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

What Am I Doing Wrong? - Python

Chunky1106
Member

Posts: 5
Location:
Joined: 29.10.08
Rank:
HBH Guru
Posted on 08-01-10 10:47
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
Posted on 08-01-10 12:08
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
Posted on 08-01-10 13:58
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
Posted on 08-01-10 14:49
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.
Guest
Username

Password

Remember Me


Bookmark This Page
Affiliates
Adverts

 

 

Links
By using, viewing or obtaining any information contained on this site, you agree to the disclaimer.

© HellBound Hackers 2008- 2009. Since 3rd December 2004.