Python Forum
Why is the while loop not working?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is the while loop not working?
#1
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.
Reply
#2
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:")
Reply
#3
(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.
Reply
#4
(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]
Reply
#5
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:
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  while loop not working-I am using sublime text editor mma_python 4 1,149 Feb-05-2023, 06:26 PM
Last Post: deanhystad
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,496 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  Infinite loop not working pmp2 2 1,661 Aug-18-2020, 12:27 PM
Last Post: deanhystad
  Loop not working Nonameface 8 2,947 Jul-19-2020, 12:27 PM
Last Post: snippsat
  for loop script over telnet in Python 3.5 is not working abhijithd123 1 2,906 May-10-2020, 03:22 AM
Last Post: bowlofred
  How to keep a loop containing a web import working in python executable? coder1384 3 2,890 Feb-22-2020, 06:49 PM
Last Post: snippsat
  For loop not working pjgrah01 4 2,823 Nov-03-2019, 11:58 PM
Last Post: pjgrah01
  Appending to list not working and causing a infinite loop eiger23 8 4,007 Oct-10-2019, 03:41 PM
Last Post: eiger23
  Working on my FOR loop wilsonrivas 1 2,218 May-24-2019, 04:05 PM
Last Post: heiner55
  plotting inside the loop is not working jenya56 4 3,098 Apr-10-2019, 08:11 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020