Home > Mobile >  problem with code not being able to go to else block
problem with code not being able to go to else block

Time:12-21

I just want to be able to move onto the else block if the length of the list is greater than 1 person as of course, what I say to if only one person is coming will not be appropriate to say if 2 or more are coming

guests = []
guests_potential = []

attendence_question = input("Are you going to be coming to my house?\n")

while attendence_question == 'yes':
        name = input("Ok! Can I please have your name?\n" )
        guests.append(name)
        #guests_integer = " ".join(guests)
        if len(guests) >= 1: 
            print("Great! So far it is only you that is coming")
            print("----------------")
            attendence_question = input("Hi!\nHow about you?\nAre you going to be coming to my house?\n")
            guests.append(name)
            
        else:
            if len(guests) < 1:
                for number, guest in enumerate(guests, 1):
                    print(number, guest)

guests_potential.append(name)
print("Ok! I've added your name list to the potential attendees!")

CodePudding user response:

Your if statement is always being called because it is checking if there is at least 1 name in the guest list. This is always the case because on the previous line you add a name to the guest list.

Instead, just check if there is 1 guest, otherwise do the else loop:

while attendence_question == 'yes':
   name = input("Ok! Can I please have your name?\n" )
   guests.append(name)

   if len(guests) == 1: 
       print("Great! So far it is only you that is coming")
       print("----------------")            
   else:
       print("The current guest list is:")
       for number, guest in enumerate(guests):
           print(number, guest)
   
   attendence_question = input("Hi!\nHow about you?\nAre you going to be coming to my house?\n"))

CodePudding user response:

Your else block will never do anything visible, because it contains an if statement to check that there's less than one guest (so, zero) and then loops over those zero guests

  • Related