944,098 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 567
  • Python RSS
Nov 3rd, 2009
0

Need help please!

Expand Post »
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Norua is offline Offline
3 posts
since Nov 2009
Nov 3rd, 2009
1
Re: Need help please!
You should try to post some code yourself.

Can give you some tips.
Python Syntax (Toggle Plain Text)
  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. >>>

Python Syntax (Toggle Plain Text)
  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; Nov 3rd, 2009 at 8:44 pm.
Reputation Points: 280
Solved Threads: 278
Master Poster
snippsat is online now Online
771 posts
since Aug 2008
Nov 3rd, 2009
0
Re: Need help please!
Thats the thing, I'm so lost at this point in the class.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Norua is offline Offline
3 posts
since Nov 2009
Nov 3rd, 2009
1
Re: Need help please!
One more,now start to think how to put this together.
Python Syntax (Toggle Plain Text)
  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; Nov 3rd, 2009 at 9:07 pm.
Reputation Points: 280
Solved Threads: 278
Master Poster
snippsat is online now Online
771 posts
since Aug 2008
Nov 3rd, 2009
0
Re: Need help please!
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Norua is offline Offline
3 posts
since Nov 2009
Nov 4th, 2009
0
Re: Need help please!
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; Nov 4th, 2009 at 2:00 am.
Reputation Points: 10
Solved Threads: 19
Junior Poster
ShadyTyrant is offline Offline
120 posts
since Nov 2009
Nov 4th, 2009
0
Re: Need help please!
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
Reputation Points: 12
Solved Threads: 14
Junior Poster in Training
baki100 is offline Offline
79 posts
since Apr 2009
Nov 4th, 2009
0
Re: Need help please!
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.
Python Syntax (Toggle Plain Text)
  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. '''
Reputation Points: 280
Solved Threads: 278
Master Poster
snippsat is online now Online
771 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Please help me get past this problem
Next Thread in Python Forum Timeline: Importing Graphics Module





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC