Python Forum
if /else statement not executing correctly - 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: if /else statement not executing correctly (/thread-17557.html)



if /else statement not executing correctly - bluethundr - Apr-15-2019

I am trying to get a list of groups in an aws account. Then allow a user to add the groups to a user name, until the user enters quit:
group_list = client.list_groups()
all_groups = []
for group in group_list['Groups']:
    group_name = group['GroupName']

all_groups.append(group_name)
    for group_name in all_groups:
        print(group_name)

add_group_name = ''
while add_group_name != 'quit':
    add_group_name = input("Enter the group name to add to user %s: " % user_name)
    if add_group_name == 'quit':
    break
else:
    print("Add user to group.")
    client.add_user_to_group(GroupName=add_group_name,UserName=user_name)
What happens is that the line:
client.add_user_to_group(GroupName=add_group_name,UserName=user_name)
never gets executed. How can I change this so thtat this line gets executed? Also is there a better way of doing this?


RE: if /else statement not executing correctly - Larz60+ - Apr-15-2019

line 8 and 14 indent is incorrect
change line 13 to:
if add_group_name.strip() == 'quit':
on second look:
remove lines 13 and 14 entirely
change line 11 to:
while add_group_name.strip() != 'quit':
and move lines 16 & 17 outside of while loop


RE: if /else statement not executing correctly - Yoriz - Apr-15-2019

The else is not indented to be inline with the if

group_list = client.list_groups()
all_groups = []
for group in group_list['Groups']:
    group_name = group['GroupName']
 
all_groups.append(group_name)
for group_name in all_groups:
    print(group_name)
 
add_group_name = ''
while add_group_name != 'quit':
    add_group_name = input("Enter the group name to add to user %s: " % user_name)
    if add_group_name == 'quit':
        break
    else:
        print("Add user to group.")
        client.add_user_to_group(GroupName=add_group_name,UserName=user_name)



RE: if /else statement not executing correctly - bluethundr - Apr-15-2019

(Apr-15-2019, 08:58 PM)Larz60+ Wrote: line 8 and 14 indent is incorrect
change line 13 to:
if add_group_name.strip() == 'quit':
on second look:
remove lines 13 and 14 entirely
change line 11 to:
while add_group_name.strip() != 'quit':
and move lines 16 & 17 outside of while loop


Thank you very much! That worked perfectly. Now I have a much better idea of how to do this. Have a great day!