Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while loops
#1
Hello,

Is there anybody who can help with a problem with the while loops?
__________________________________________________________________
I'm trying to code the following task: creation of a booking system which has to exclude 3 banned names (John, Tom, Jane).
The steps that I had to do are the following:

1- Define the list of banned users: "John", "Tom", "Jane".

2- Create an empty list of members of the guests party.

3- Using an infinitely running while loop and if statements, process the following user inputs:

3a- “Done” - Program should print “Your final group of guests is: {list of guests}” and then exit the infinite loop.

3b- "Any other input (names)" - Program should check whether the guests name is in the banned list.

3b1- If the guest is not in the banned list, the program should add this guest to the list of guests in the party and then print "{name} is allowed in, welcome! Full list of guests is now: {list of guests}".

3b2- If the guest is on the banned list, the program should not add them to the list of guests in the party and should print "{name} is banned! They cannot come in!".
_____________________________________________________________
Example 1
Input:
- John
- Mary
- Bill
- Tom
- Simon
- Done
Desired output:
- John is banned! They cannot come in!
- Mary is allowed in, welcome! Full list of guests is now: [‘Mary’]
- Bill is allowed in, welcome! Full list of guests is now: ['Mary’, ‘Bill’]
- Tom is banned! They cannot come in!
- Simon is allowed in, welcome! Full list of guests is now: [Mary’, 'Bill’, ‘Simon’]
- Your final group of guests is: ['Mary’, 'Bill’, ‘Simon’]
_____________________________________________________________
Example 2:
Input:
- John
- John
- John
- Done
Desired output:
- John is banned! They cannot come in!
- John is banned! They cannot come in!
- John is banned! They cannot come in!
- Your final group of guests is: []
_____________________________________________________________

So far I did the following, but if a banned name is added more than one time, the loop for excluding the name stops after the first iteration and it messes up the allowed users.

# Creation of an empty list of all users and ask to all of them the name until done
users = []
user = ''
while user != 'Done':
    user = input("Please write your name here: ").title().strip()
    if user != 'Done':
        users.append(user)
print(f'\nGuests requiring to enter: {users}')
print(   )

# Screening of all users and identification of banned users
banned_users = ['John', 'Tom', 'Jane']
for name in banned_users:
    if name in users:
        print(f'{name} you are banned! you cannot come in!')
        users.remove(name)
        continue
    else:
        print(f'{users} you are allowed in, welcome! Full list of guests is now:')
print(   )

print(f'\n- Your final group of guests is:{users}')
_____________________________________________________________

Does anybody have a solution for this?
Thanks!!
Reply
#2
More of your code should be indented inside of the while loop.
You are not actually following the requirements of the steps, read the steps again and adjust the code to do what is actually stated.
Paulman likes this post
Reply
#3
From the docs:

https://docs.python.org/3/tutorial/datastructures.html
Quote:list.remove(x)
Remove the first item from the list whose value is equal to x. It raises a ValueError if there is no such item.

So if a list contains multiple x's, how do you remove them all? You already have the answer for 1, and you've shown that you know how to do something multiple times.
Paulman likes this post
Reply
#4
If the steps are followed correctly there will be nothing to remove.
Paulman likes this post
Reply
#5
True. Banned guests should be culled in step 3. You are adding a step 4. There should only be 1 loop in this program.
Paulman likes this post
Reply
#6
Indeed, the instructions suggest one loop with exclusion of the bad names as soon as they are entered. But, your example desired output suggests that all names are added and then a second loop goes through that list and does the exclusions.

So which is it?

If the latter, I would build a second list of names from the first, adding a name only if it is not in the bad name list.
good_list = []
for name in users:
    if name not in banned_users:
        good_list.append(name)
Paulman likes this post
Reply
#7
Thanks everybody, your points were really helpful and I understood were I was messing up.

I report here below the final code for the records, now it looks working as required:

# Creation of an empty list of all users and ask to all of them the name until done
users = []
user = ''
while user != 'Done':
    user = input("Please write your name here: ").title().strip()
    if user != 'Done':
        users.append(user)
print(f'\nGuests requiring to enter: {users}')
print(   )

# Screening of all users and identification of banned users
banned_users = ['John', 'Tom', 'Jane']
for name in banned_users:
    if name in users:
        print(f'{name} you are banned! you cannot come in!')
        users.remove(name)
        continue
    else:
        accepted_users = []
        for name in users:
            if name not in banned_users:
                accepted_users.append(name)
                print(f'{name} you are allowed in, welcome! Full list of guests is now: {accepted_users}')
print(   )

print(f'\n- Your final group of guests is:{accepted_users}')
Thanks again, really appreciated!!
Reply
#8
No, wrong. Once a name is entered you should immediately perform the steps 3b*. So inside the while loop.
There should not be a second for loop running after all names are entered.
Paulman and Ghazi like this post
Reply
#9
1) please define the banned list at the top of your program
2) you do not need continue inside the if statement
3)I suggest iterating over index, and use pop() method so that you are able to remove all the occurrences of banned names in the other list
I hope these tips will help you, I am not sure if I am allowed to spoonfeed you.

Smile
Paulman likes this post
Reply
#10
Hi all, thanks for the inputs. I followed your suggestions and I thought that the coding was working, but I found another problem (hopefully the last). Accordingly to the last revision (below) the code looks working well except for one thing: the list of the accepted_users repeats itself as many times as the number of accepted_users. Practically if (out of the banned_users) I put e.g. other 3 names, I'll get the list of the accepted_users with the 3 names repeated 3 times (if 4 names, 4 times and so on). Does anybody have a clue on this problem?

# Creation of an empty list of all users and ask to all of them the name until done
users = []
user = ''
while user != 'Done':
    user = input("Please write your name here: ").title().strip()
    if user != 'Done':
        users.append(user)
print(   )

# Screening of all users and identification of banned users
banned_users = ['John', 'Tom', 'Jane']
accepted_users = []
for name in users:
    if name in banned_users:
        banned_users.append(name)
        print(f'{name} is banned! They cannot come in!')
        banned_users.remove(name)
    else:
        for name in users:
            if name not in (banned_users):
                accepted_users.append(name)
                print(f'{name} is allowed in, welcome! Full list of guests is now: {accepted_users}')
print(   )

# List of final group of accepted guests
print(f'\nYour final group of guests is: {accepted_users}')
Reply


Forum Jump:

User Panel Messages

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