Oct-11-2020, 05:14 PM
(This post was last modified: Oct-11-2020, 05:23 PM by Yoriz.
Edit Reason: Added code tags
)
Here's my assignment:
Odd/Even Counter
In this chapter you saw an example of how to design an algorithm that determines whether a number is even or odd (see Returning Boolean Values in Section 6.2). 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.
Here's my code:
HERE'S my question. So my program has random numbers 0-1000, how can I pick 100 random numbers from this group of 1001 numbers using the code I have? We aren't to lists and things like this, we've basically covered, loops and functions. Thanks for any ideas you have.
Odd/Even Counter
In this chapter you saw an example of how to design an algorithm that determines whether a number is even or odd (see Returning Boolean Values in Section 6.2). 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.
Here's my code:
HERE'S my question. So my program has random numbers 0-1000, how can I pick 100 random numbers from this group of 1001 numbers using the code I have? We aren't to lists and things like this, we've basically covered, loops and functions. Thanks for any ideas you have.
import random numbers=random.randint(0,1000) countEven = 0 countOdd = 0 for x in range(100): if not x % 2 countEven += 1 else: countOdd += 1 print('Number of even numbers :',countEven) print('Number of odd numbers: ', countOdd)