1

I am having a dilemma with my code, first of there was an exercise i had to write that will achieve the following

Exercise

Create a file in order to complete this exercise. Write a program that generates 100 random numbers (in the range of 1-1000) and keeps a count of how many of those random numbers are even and how many are odd. Display the results to the screen as seen in the sample output below. Hint: Use a while loop to loop 100 times.

My result:

import random
num = 0
odd = 0
even = 0

while num < 100:
    random.randint(1,1000)
    num = num + 1
    #print(num)
    if random.randint(1,1000)%2==0:
        even = even + 1
    else:
        odd = odd + 1

print ("Out of 100 Random Numbers,",even,"were even and",odd,"were Odd")

Output:

Out of 100 Random Numbers, 50 were even and 50 were Odd

All Gravy !!

Next activity:

Add another while loop which repeats part a 10 times. Display the results to the screen as seen in the sample output below.

With an output like this:

Out of 100 random numbers, 56 were odd, and 44 were even.
Out of 100 random numbers, 60 were odd, and 40 were even.
Out of 100 random numbers, 47 were odd, and 53 were even.
Out of 100 random numbers, 54 were odd, and 46 were even.
Out of 100 random numbers, 48 were odd, and 52 were even.
Out of 100 random numbers, 53 were odd, and 47 were even.
Out of 100 random numbers, 46 were odd, and 54 were even.
Out of 100 random numbers, 52 were odd, and 48 were even.
Out of 100 random numbers, 53 were odd, and 47 were even.

So I have written:

import random
num = 0
odd = 0
even = 0
loop = 0


while loop < 10:
    loop = loop +1

    while num < 100:
        num = num + 1
        rand = random.randint(1,1000)
        #print(num)
        if rand%2==0:
            even = even + 1
        else:
            odd = odd + 1
    result = print ("Out of 100 Random Numbers,",even,"were even and",odd,"were Odd")

resulting in Output:

Out of 100 Random Numbers, 50 were even and 50 were Odd
Out of 100 Random Numbers, 50 were even and 50 were Odd
Out of 100 Random Numbers, 50 were even and 50 were Odd
Out of 100 Random Numbers, 50 were even and 50 were Odd
Out of 100 Random Numbers, 50 were even and 50 were Odd
Out of 100 Random Numbers, 50 were even and 50 were Odd
Out of 100 Random Numbers, 50 were even and 50 were Odd
Out of 100 Random Numbers, 50 were even and 50 were Odd
Out of 100 Random Numbers, 50 were even and 50 were Odd
Out of 100 Random Numbers, 50 were even and 50 were Odd

Can one of you fine programmers explain to me why I am getting that result and/or modify the code the get the expect result for the exercise.

An explanation of why its not working and how to fix it would be preferable because you know what they say, give a man a fish he will eat for a day teach him to fish he will eat for a lifetime.

Thanks for your time.

1
  • Inner while won't be executed after first loop as it will reach 100 at that point. You have to initialize the num value inside the first loop. Commented Mar 28, 2013 at 10:16

6 Answers 6

3

Try to update your code this way(move num, odd, even initialization inside of top while):

from random import randint

loop = 0

while loop < 10:
    num = 0
    odd = 0
    even = 0
    loop += 1

    while num < 100:
        num += 1
        rand = randint(1, 1000)
        if rand % 2 == 0:
            even += 1
        else:
            odd += 1
    print "Out of 100 Random Numbers, {0} where even and {1} where Odd".format(even, odd)
Sign up to request clarification or add additional context in comments.

Comments

0

You do not reset any of your counters after each pass of the inner loop. So, the first time through, it counts the odds and evens for that run, until num gets to 100. The second time through, when it gets to the start of the while loop it checks the value of num: it's already 100, so it never even enters the loop, and just prints the counts from the previous time.

So one way of fixing it would be to move your setting of num, odd and even inside the outer loop.

All that said, using while loops is a bit odd here. Really you should look into for loops, which are much better for this purpose.

3 Comments

Great, all responses were good however your responses explained it to me best so thanks, i feel like such a derp for not figuring it out in the first place.
Thanks. Don't be so hard on yourself, though: you're doing fine for a newcomer to programming. It takes some time to get into the habit of thinking like a computer.
Thanks :D, yes i would also have chosen a for loop for this however i am following the practicals my lecture gave me.
0

You need to move the following inside the outer loop:

num = 0
odd = 0
even = 0

Otherwise the second and subsequent iterations of the inner loop are effectively no-ops.

Comments

0

You've forgotten to reset the value of num (and also even and odd) inside the outer while loop (before starting the while num < 100 loop). Second time in, it doesn't need to execute the inner loop or calculate any random numbers at all because num is already 100

1 Comment

also "were", not "where" or "Where" !
0

the variable num=100 after termination of while loop so u need to reset num=0 with in the outer while loop

Comments

0

Just declare odd,even,num to be 0, at the start of the first while loop.
That way you will reset these variables each time, giving you the output you want.

You should end up with

while loop < 10:
    loop = loop +1
    num = even = odd = 0 # here
    while num < 100:
        num = num + 1
        rand = random.randint(1,1000)
        #print(num)
        if rand % 2 == 0:
            even = even + 1
        else:
            odd = odd + 1
        # grammar
     print ("Out of 100 Random Numbers,",even,"were even and",odd,"were Odd")

Another version that prints the total number of generated numbers

loop = 0
even = odd = 0 # change 1
while loop < 10:
    loop = loop +1
    num = 0 # change 2
    while num < 100:
        num = num + 1
        rand = random.randint(1,1000)
        #print(num)
        if rand % 2 == 0:
            even = even + 1
        else:
            odd = odd + 1
        # grammar
    print ("Out of", loop*100,"Random Numbers,",even,"were even and",odd,"were Odd")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.