You are on page 1of 2

Starting Out with Python

Here is a collection of easier problems for those of you just starting out with Python. These
problems will be created by me and grade 11's and 12's in the club to help get you started with
the style of competition problems.

1. Write a program that generates a series of random integers between 0 and 100. The program
should add all the odd numbers together and show the sum at the end.

Sample Output

11
8
3
21
4
0
2
12
96
43

Sum is 78

Hints: use import random as the first line of code and also use the command
random.randint(0,100) to generate a random integer in the appropriate range. You could use a
for loop or while loop.

Solution:

#add_random_odd.py
import​ random
sum ​=​ 0
print​ ​(​"Hello world!")
for​ c ​in​ range​(​0​,​10​):
temp ​=​ random​.​randint​(​0​,​100)
if​ ​(​temp ​%​ ​2​ ​==​ ​1​):
print​ ​(​temp​,​"is an odd number")
sum ​=​ sum ​+​ temp
else:
print​ ​(​temp)
print​ ​(​"the sum of the odd numbers is"​,​sum)

You might also like