Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

TuringTest

  • 3 years ago

Beginner Python help: What does n-=1 do in a while loop?

  • This Question is Closed
  1. TuringTest
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    num = 5 if num > 2: print(num) num -= 1 print(num) output: 5,4

  2. TuringTest
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I thought num-=1 would subtract one from n each time through the loop, but then why no 3 printed?

  3. TuringTest
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    actually by my reasoning if that were true it should print out the 2 as well...

  4. TuringTest
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    version 2.7 if that matters

  5. TuringTest
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    oh never mind, I think I know

  6. TuringTest
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    it's not even a while loop, it's an if clause, so it's only executed once. duh.

  7. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy