0
cat = True
while cat = True:
    print("cat")
else:
    print("Kitten")

I tried to run this program but it says invalid syntax for the while loop.I don't know what to do and I can't find the answer on the internet.

2
  • 3
    Welcome to Raspberrry Pi SE. This very general Python question is not really a question for Raspberry Pi SE. See Tour for more information. Re your question: there is no while...else in Python, and for a comparison you need ==. I suggest you have a look online at one of the many Python tutorials. – Dirk Dec 1 '18 at 9:34
  • 2
    while ... else is actually valid ! – CoderMike Dec 1 '18 at 9:41
3

You've used the assignment operator = when testing for True. You should be using the comparison operator == to compare cat and True.

cat = True
while cat = True:
   print("cat")
else:
   print("Kitten")

The above code will output:

while cat = True:
          ^ SyntaxError: invalid syntax

Whereas:

cat = True
while cat == True:
   print("cat")
else:
   print("Kitten")

Will give the result you are probably expecting:

cat
cat
cat
...
1
  • 1
    while cat: would be better code (but the code is nonsense), and the question is not directly related to the Raspberry Pi – Milliways Dec 1 '18 at 11:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.