1

it's just simple squaring by repetitive addition. hope u guys might spare some time helping a brother out. totally missing something here because the while loop works:

x = 7
ans = 0
itersLeft = x
while (itersLeft != 0):
    ans = ans + x
    itersLeft = itersLeft - 1
print (str(x) + '*' + str(x) + ' = ' + str(ans))

but the "while True" loop fails:

x = 5
ans = 0
itersLeft = x
while True:
    if itersLeft != 0:
        ans = ans + x
        itersLeft = itersLeft - 1
print (str(x) + '*' + str(x) + ' = ' + str(ans))
3
  • 2
    What do you mean by “fails”? Also I recommend tagging the language you are using so that stackoverflow can style the colors of your code and so other users know what you’re working with here. Commented Dec 24, 2020 at 8:52
  • done deal. sorry. i thought i've tagged it already. must've missed it. anyways, by "fails" i meant, it didn't return anything on my interactive window whereas the former did. Commented Dec 24, 2020 at 8:56
  • Much better 👍🙌 Commented Dec 24, 2020 at 9:18

1 Answer 1

2

You never break the loop when you use while True so it doesnt fail it just can not print anything put an else statement and a break inside the while true it should work just fine

Sign up to request clarification or add additional context in comments.

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.