Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
July 28th, 2012, 10:49 PM
 grimofdoom
Registered User

Join Date: Jul 2012
Posts: 2
Time spent in forums: 15 m 38 sec
Reputation Power: 0
Help!??

I am fairly new to python, so i thought i would mess around and make a command program. What it does is send out 200 numbers randomly generated of 1's and 2's and then counts them and displays the outcome and the total number count. I was wondering if someone could help me because i want the outcome and the print line on one line. here is my script:
Code:
```total=0
sone=0
stwo=0

while total<200:
import random
num = random.randint(1,2)
print num
total=total+1
if num==2:
stwo=stwo+1
if num==1:
sone=sone+1
if total==200:
print "This program is about to start counting the 1's and 2's"
print "this program will now count the total of 1's and 2's"
print ""
print ""
print "*****************************************************************************"
print "twos were found in this run,the total count of ones are:"
print sone
print "---"
print"the total 2's that show up are:"
print stwo
print "---"
print "the total number of 1's and 2's used is :"
print total```

#2
July 28th, 2012, 11:25 PM
 grimofdoom
Registered User

Join Date: Jul 2012
Posts: 2
Time spent in forums: 15 m 38 sec
Reputation Power: 0
I made an edit to my file and now it doesnt work, the raw data seams to not send the data right or something:
Code:
```total=0
sone=0
stwo=0

var=raw_input("Enter a number: ")
while total<var:
import random
num = random.randint(1,2)
print num
total=total+1
if num==2:
stwo=stwo+1
if num==1:
sone=sone+1
if total==var:
print "This program is about to start counting the 1's and 2's"
print "this program will now count the total of 1's and 2's"
print ""
print ""
print "*****************************************************************************"
print "twos were found in this run,the total count of ones are:"
print sone
print "---"
print"the total 2's that show up are:"
print stwo
print "---"
print "the total number of 1's and 2's used is :"
print total```

#3
July 29th, 2012, 07:28 AM
 Quackajack
Contributing User

Join Date: Jul 2012
Posts: 39
Time spent in forums: 10 h 52 m 39 sec
Reputation Power: 2
I think the answer you are looking for is use a comma to separate the text and and the value on the print line. For example

Code:
```print "Total count of ones are:", sone
print "---"
print "Total 2's that show up are:", stwo
print "---"
print "Total number of 1's and 2's used is :", total```

PS You don't need the import line in the loop; move it to the top. You are actually calling import random 200 times; Python will know it has already imported it before and skip the last 199 requests.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Help!??