0
a=1

while a<=100:
    if a % 3 == 0:
         print("foo")
    elif a % 5 == 0:
         print("bar")

    print(a)

    a=a+1

My output would print:

 1
 2
 foo
 3
 4
 bar
 5
 6

Instead of the output to be number 3, it should display the string (replace it)

how could I code a command that will replace the int with a string?

4
  • 1
    else is your friend Commented Feb 16, 2015 at 21:11
  • You do realize that multiples of both 3 and 5 will only print foo. Is that what you intend? Commented Feb 16, 2015 at 21:13
  • (for a in range(1,101) could be your friend too. Or for a in range(1, 101, 5)) Commented Feb 16, 2015 at 21:14
  • Why are you not using a for loop? Commented Feb 16, 2015 at 21:37

2 Answers 2

1

Add an else clause:

a=1

while a<=100:
    if a % 3 == 0:
         print("foo")

    elif a % 5 == 0:
         print("bar")

    else:
        print(a)

    a=a+1
Sign up to request clarification or add additional context in comments.

Comments

1
a =list(range(1,101))

for i in a :

if i % 3 and i % 5 == 0:
  print(foobar)
elif i % 3 == 0 :
  print(foo)
elif i % 5 == 0 :
  print(bar)
else:
  print(i)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.