Python Forum
Why is the while loop not working? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Why is the while loop not working? (/thread-29101.html)



Why is the while loop not working? - mcoliver88 - Aug-18-2020

Hi,

Any idea why the while loop is not working?

homework = 'n'
dinner = 'n'
flag = False
while (homework == 'n' or homework == 'N') and (dinner == 'n' or dinner == 'N'):
  if flag == True:
    print("Sorry you cant play xbox, lets try again:")
  homework = ("Did Harry complete his homework? (y/n")
  dinner = ("Did harry eat his dinner? (y/n")
  flag = True
else:
  print("Great, well done Harry you can go and play Xbox")
Thank you in advanced.

Just noticed a mistake in my code, but still doesn't work:

homework = 'n'
dinner = 'n'
flag = False
while (homework == 'n' or homework == 'N') and (dinner == 'n' or dinner == 'N'):
  if flag == True: # when flag is true show the below statement
    print("Sorry you cant play xbox, lets try again:")
  homework = input("Did Harry complete his homework? (y/n): ")
  dinner = input("Did harry eat his dinner? (y/n): ")
  flag = True # will set flag to true
else:
  print("Great, well done Harry you can go and play Xbox")
when I enter y for the first question and n for the second, it then proceeds to show the else statement, rtaher than loop it as intended.


RE: Why is the while loop not working? - Cryptus - Aug-18-2020

because after you give homework = yes and dinner = no the while function isnt true anymore and it jumps to your else function

I would do something like this
homework = 'n'
dinner = 'n'
flag = False
while (homework == 'n' or homework == 'N') and (dinner == 'n' or dinner == 'N'):
    homework = input("Did Harry complete his homework? (y/n): ")
    dinner = input("Did harry eat his dinner? (y/n): ")
    if (homework == 'y' or homework == 'Y') and (dinner == 'y' or dinner == 'Y'):
        flag = True  # will set flag to true
else:
    if flag == True:  # when flag is true show the below statement
        print("Great, well done Harry you can go and play Xbox")
    else:
        print("Sorry you cant play xbox, lets try again:")



RE: Why is the while loop not working? - mcoliver88 - Aug-18-2020

(Aug-18-2020, 01:46 PM)Cryptus Wrote: because after you give homework = yes and dinner = no the while function isnt true anymore and it jumps to your else function

Thanks for the advice.

I am new to Python, so what's the best way to solve this? I thought that the conditions would need to be satisfied in order for the loop to be marked as completed.


RE: Why is the while loop not working? - Cryptus - Aug-18-2020

(Aug-18-2020, 01:49 PM)mcoliver88 Wrote:
(Aug-18-2020, 01:46 PM)Cryptus Wrote: because after you give homework = yes and dinner = no the while function isnt true anymore and it jumps to your else function

Thanks for the advice.

I am new to Python, so what's the best way to solve this? I thought that the conditions would need to be satisfied in order for the loop to be marked as completed.

I would suggest you to debug your programm to see exactly how and why it loops I am working with pycharm and if you are on visual studio code I dont really know how to debug there but on pycharm there is a button for it

[Image: 65M6iLn.png]


RE: Why is the while loop not working? - jefsummers - Aug-18-2020

Did you mean to code a while/else loop, or did you mean to have the else tied to the if statement?

Also, nitpicking,
if flag == True
is redundant. You can just say
if flag:



RE: Why is the while loop not working? - deanhystad - Aug-18-2020

Think about the logic. Currently you have

While Harry has not finished his Homework and Harry has not eaten his dinner
   No Xbox for Harry
Harry can play Xbox
What happens if Harry ate dinner but didn't finish his homework?

Harry has not finished his Homework = True
Harry has not eaten his Dinner = False
True and False == ?

Also, using else after a while statement is odd. You would use "else" if you wanted to differentiate exiting the loop because the while conditions were met from exiting the loop because of a "break". If you aren't using "break" you are just going to confuse anyone reading your code. Using else with a for loop is exactly the opposite. In a for loop the else code is only executed when the loop is terminated by a "break". So much for consistency. But nobody uses "else" with "while" and neither should you.