Anirudh Gottiparthy Anirudh Gottiparthy - 2 months ago 121
Python Question

Python While Loops Asterisk

Re-type and run, note incorrect behavior. Then fix errors in the code, which should print num_stars asterisks.

while num_printed != num_stars:
print('*')


Below is the code which I have entered. I am getting an infinite loop hence no answer. Please help me out! Thanks!

num_stars = 3

num_printed = 0

while(num_printed != num_stars):

print(num_stars*'*')

Answer

You'll want either:

num_stars = 3
print(num_stars * '*')

or:

num_stars = 3
num_printed = 0
while(num_printed < num_stars):
    print('*')
    num_printed += 1

Depending on what you're trying to do.

Comments