Home > Net >  How can I make it so that the loop keeps repeating until a all the elements in the list are answered
How can I make it so that the loop keeps repeating until a all the elements in the list are answered

Time:06-30

I am trying to make it so that the question or sequence keeps getting repeated until user has named every day of the week and prints a message that congratulates the user after that. I am trying to use a while loop but I can't seem to get it to work. I have also tried using an if statement although it does work, it doesn't repeat the question and keep adding to the list continously.

This is the output that I am trying to get... enter image description here

days = []
days2 = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
answer = input('Enter a day of the week: ')

while answer in days2:
        days.append(answer)
        print('%s has been added to our days of the week.' % answer)
        print(days)

if answer in days:
    print('Sorry already in our list.')
if answer
else:
    print('Sorry %s is not a day of the week.' % answer)

This is what I have so far

Thank youu :D

CodePudding user response:

Your current loop structure doesn't make sense. Your validity test does look good!

Here is the high-level outline you want to strive for:

while fewer than seven elements in days:
    solicit a new day from the user
    figure out if it's valid - correct spelling and not present
    if it's a valid day:
        tack it onto the list

At some point, a patient user will enter all the days and len() will return 7, allowing the while loop to terminate.

CodePudding user response:

There are several ways to do this, but here is one that seems to be in the spirit of what you are going for.

days = []
days2 = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']

while sorted(days) != sorted(days2):
    answer = input('Enter a day of the week: ').lower()
    if answer in days:
        print('Sorry already in our list.')     
    elif answer not in days2:
        print('Sorry %s is not a day of the week.' % answer)
    else:        
        days.append(answer)
        print('%s has been added to our days of the week.' % answer)
        print(days)

print("you got them all!")

I'd suggest looking to Python flow control, using things like next, for, while, break, etc. Note the lower() as well, since Python cares about case and I would guess you want Monday treated the same as monday. Also when comparing lists it cares about the order, so I just sorted each one.

CodePudding user response:

First of all, if statement is out of the loop, and your loop will immediately exit if a user enters a value, not found in days2. Ie. if a user enters value monday, while loop will be skipped and the program will continue executing reaching an if part.

On the other hand, if a user enters a value that is found in list days2 for the first time, your program will continue running in the while loop until user enters something that is not in the list days2.

Instead of a list, you might need to consider using set object. One of the correct answers, adapting from your code would be this:

days = set()
# this is also a set
days2 = {'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'}

# let's avoid a problem where a user can exit the program from the beginning
# therefore, this should be inside the loop
# answer = input('Enter a day of the week: ')

while len(days) < 7:
    answer = input('Enter a day of the week: ')
    if answer in days2 and answer in days:
        print('Sorry already in our list')
    elif answer not in days2:
        print('Sorry %s is not a day of the week.' % answer)
    else:
        days.add(answer)
        print('%s has been added to our days of the week.' % answer)
        print(days)

CodePudding user response:

There are several decent answers on here already, but I took a slightly different approach to the problem:

days_of_week = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
entered_days = []

while days_of_week != []:
    answer = input('Enter a day of the week: ').casefold()
    if((answer in days_of_week) and not(answer in entered_days)):
        entered_days.append(answer)
        days_of_week.remove(answer)
        print('%s has been added to our days of the week.' % answer.capitalize())
        print(entered_days)
    elif answer in entered_days:
        print('Sorry already in our list.')
    else:
        print('Sorry %s is not a day of the week.' % answer)
print("Well done! You have populated the list.")
  • Related