File tree Expand file tree Collapse file tree 4 files changed +41
-0
lines changed Expand file tree Collapse file tree 4 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ We will use this script to teach Python to absolute beginners
3+ The script is an example of reading the numbers from file in Python
4+
5+ The problem is:
6+ For all integers between 1 and 99 (include both):
7+ # Read the input 3,5 and 99 from the input file
8+ # print fizz for multiples of 3
9+ # print buzz for multiples of 5
10+ # print fizzbuzz for multiples of 3 and 5"
11+ """
12+
13+ def fizzbuzz (max_num ):
14+ "This method implements FizzBuzz"
15+
16+ # adding some redundant declarations on purpose
17+ # we will make our script 'tighter' in one of coming exercises
18+ three_mul = 'fizz'
19+ five_mul = 'buzz'
20+ with open ('mifile.txt' ,'r' ) as f :
21+ print 'i have created'
22+ num1 = int (f .readline ())
23+ num2 = int (f .readline ())
24+ max_num = int (f .readline ())
25+
26+ # Google for 'range in python' to see what it does
27+ for i in range (1 ,max_num ):
28+ # % or modulo division gives you the remainder
29+ if i % num1 == 0 and i % num2 == 0 :
30+ print (i ,three_mul + five_mul )
31+ elif i % num1 == 0 :
32+ print (i ,three_mul )
33+ elif i % num2 == 0 :
34+ print (i ,five_mul )
35+
36+ #----START OF SCRIPT
37+ if __name__ == '__main__' :
38+ fizzbuzz (100 )
Original file line number Diff line number Diff line change 1+ 3
2+ 5
3+ 99
You can’t perform that action at this time.
0 commit comments