Need help please!

Thread Solved

Join Date: Nov 2009
Posts: 3
Reputation: Norua is an unknown quantity at this point 
Solved Threads: 0
Norua Norua is offline Offline
Newbie Poster

Need help please!

 
0
  #1
20 Days Ago
I'm having trouble with my lab for a computer class. The problem is as stated:
Design a program that generates 100 random numbers, and keeps a count of how many of those random numbers are even and how many are odd.

Can someone please help me out with this?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 146
Reputation: snippsat is an unknown quantity at this point 
Solved Threads: 42
snippsat snippsat is offline Offline
Junior Poster
 
1
  #2
20 Days Ago
You should try to post some code yourself.

Can give you some tips.
  1. >>> for i in range(0,7):
  2. a = random.randint(0,100)
  3. print a
  4.  
  5. 79
  6. 21
  7. 69
  8. 60
  9. 28
  10. 68
  11. 0
  12. >>>

  1. >>>
  2. >>> 1 % 2
  3. 1
  4. >>> 2 % 2
  5. 0
  6. >>> 3 % 2
  7. 1
  8. >>> 4 % 2
  9. 0
  10. >>>
Last edited by snippsat; 20 Days Ago at 8:44 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 3
Reputation: Norua is an unknown quantity at this point 
Solved Threads: 0
Norua Norua is offline Offline
Newbie Poster
 
0
  #3
20 Days Ago
Thats the thing, I'm so lost at this point in the class.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 146
Reputation: snippsat is an unknown quantity at this point 
Solved Threads: 42
snippsat snippsat is offline Offline
Junior Poster
 
1
  #4
20 Days Ago
One more,now start to think how to put this together.
  1. >>> count = 0
  2. >>> a = 55
  3. >>> if a % 2 == 1:
  4. count += 1
  5. print 'odd', count
  6.  
  7. else:
  8. print 'even'
  9.  
  10.  
  11. odd 1
  12. >>>
Last edited by snippsat; 20 Days Ago at 9:07 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 3
Reputation: Norua is an unknown quantity at this point 
Solved Threads: 0
Norua Norua is offline Offline
Newbie Poster
 
0
  #5
20 Days Ago
I understand that part and I've been reading in my book. But I still have yet to figure out how to make it get 100 random numbers and then tell the amount of even and odd numbers.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 22
Reputation: ShadyTyrant is an unknown quantity at this point 
Solved Threads: 5
ShadyTyrant's Avatar
ShadyTyrant ShadyTyrant is offline Offline
Newbie Poster
 
0
  #6
20 Days Ago
Try not to over think the problem. You should only need 4 variables in this problem. even, odd, randNum, and i. Variable i is used to control the for loop, randNum is used to hold a random number for even or odd comparison, even and odd are incremented based on the comparison of the randNum. Then print your results to the console. Also what type of computer course are you taking?
Last edited by ShadyTyrant; 20 Days Ago at 2:00 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 41
Reputation: baki100 is an unknown quantity at this point 
Solved Threads: 10
baki100 baki100 is offline Offline
Light Poster
 
0
  #7
20 Days Ago
dude just combine everything snippsat posted he basically wrote you the programme it's right there, just think about it
create a loop from 1 to 100
generate random number and store in a variable
if the number is odd add 1 to odd variable
else add 1 to even variable
print odd and even variables
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 146
Reputation: snippsat is an unknown quantity at this point 
Solved Threads: 42
snippsat snippsat is offline Offline
Junior Poster
 
0
  #8
19 Days Ago
Next time if you post here try to post some code.
That will show that you but some effort in to it.

With homework we will not just post finish code.

Here is one soultion,as extra i put number odd/even number in a list.
Try to understand what going on,ask question if you not sure an we break it down.
  1. import random
  2.  
  3. count_odd = 0
  4. count_even = 0
  5. odd_list = []
  6. even_list = []
  7. for i in range(0,100):
  8. rand_n = random.randint(0,100)
  9. if rand_n % 2 == 1:
  10. count_odd += 1
  11. odd_list.append(rand_n)
  12. else:
  13. count_even += 1
  14. even_list.append(rand_n)
  15.  
  16. print 'Out off 100 random number %d was odd\nHere are the numbers->\n' % (count_odd)
  17. print odd_list
  18. print
  19. # Here we do a check that it`s 100 number
  20. print 'Out off %d random number %d was even\nHere are the numbers->\n' % (count_even + count_odd, count_even)
  21. print even_list
  22.  
  23. '''my output->
  24. Out off 100 random number 44 was odd
  25. Here are the numbers->
  26.  
  27. [53, 67, 27, 37, 97, 75, 89, 87, 65, 81, 31, 85, 85, 33, 13, 13, 27, 5, 75, 37, 73, 39, 53, 71, 53, 27, 23, 1, 13, 19, 29, 9, 13, 45, 19, 31, 63, 27, 33, 43, 51, 61, 35, 35]
  28.  
  29. Out off 100 random number 56 was even
  30. Here are the numbers->
  31.  
  32. [2, 2, 16, 16, 34, 14, 70, 22, 30, 2, 74, 90, 28, 36, 100, 74, 48, 8, 40, 64, 54, 94, 54, 56, 16, 4, 94, 66, 18, 12, 36, 100, 8, 18, 54, 16, 72, 16, 20, 60, 28, 6, 38, 10, 62, 88, 98, 90, 54, 26, 50, 38, 66, 76, 42, 12]
  33. '''
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC