You can use this assignment in your class!
Mastery Progress(100.0%)
i

What is wrong with the code in Figure A?

x = int(input())
if x % 3 == 0:
    print("Fizz")
elif x % 5 == 0:
    print("Buzz")
elif x % 3 == 0 and x % 5 == 0:
    print("FizzBuzz")Figure A
You answeredJUST NOW

(Student response here)

  • I want to share my response with my peers
  • Share as
    (Student name)
    Options
    • (Student name)
    • Anonymous

Did you like this question?

(Voting helps us personalize your learning experience!)
imgInstructor solution
Alec KretchAUG 11, 2021, 9:35:20 AM

Line 7 print("FizzBuzz") will never be reached.

if/elif/else statements evaluate each expression one by one. In the case where x is divisible by both 3 and 5 (i.e., 15), the first expression x % 3 == 0 will evaluate as true, and the other expressions will not be reached. In all other cases, x % 3 == 0 and x % 5 == 0 will be false.

Order matters for if/elif/else statements. Figure B is a correct implementation of the FizzBuzz code.

  • x = int(input())
if x % 3 == 0 and x % 5 == 0:
    print("FizzBuzz")
elif x % 3 == 0:
    print("Fizz")
elif x % 5 == 0:
    print("Buzz")Figure B

Was this helpful?

(Voting helps us personalize your learning experience!)
imgControl Flow in Python Presentation (page 6)

Was this helpful?

(Voting helps us personalize your learning experience!)
Think you've got it?

True or False: It is possible for this function to reach line 5 and print "ham".

  • def a_vs_b(a,b):
    if a is b:
        print("spam")
    elif a == b:
        print("ham")
    else:
        print("eggs")Figure C
Select one of the following options:
  • A.

    False

  • B.

    True

Submit answer

Was this helpful?

(Voting helps us personalize your learning experience!)

You may exit out of this review and return later without penalty.