1. Please write a program in Python 3 for the followng instructions. Please also
ID: 3745573 • Letter: 1
Question
1. Please write a program in Python 3 for the followng instructions. Please also show all outputs and share code.
Program
Write a program that prints the numbers from 1 to 150, except . . .
When you get a number that's divisible by 3, print "Fizz" instead.
When you get a number that's divisible by 5, print "Buzz" instead.
When you get a number that's divisible by 7, print "Woof" instead.
If a number is divisible by several of these values, print the appropriate words in the corresponding order. For example, for 15 you should print "Fizz Buzz", for 35 you should print "Buzz Woof", and for 105 you should print "Fizz Buzz Woof".
Consider learning how to write Python functions now, to help reduce the overall number of lines of code you write. Function come next lesson, but are one of the fundamental programming constructs in any language. Embrace them.
Explanation / Answer
for i in range(1, 151): s = '' if i % 3 == 0: s += 'Fizz' if i % 5 == 0: if s != '': s += ' ' s += 'Buzz' if i % 7 == 0: if s != '': s += ' ' s += 'Woof' if s == '': print(i) else: print(s)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.