Python Forum

Full Version: while loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i created empty list, enter a name in list by inputting user. I have placed 2 condition here.

This will ask from user to add more name after add first name. If user press y it will go into the loop and ask the same question upto 9 times. This loop is exit at 9 iteration.

If user press n at anytime this should be out of loop and print list values but this option is not working properly. Condition one is working fine but condition 2 isn't working properly.

a=[] #Empty list define
i=1 #Loop iteration set to 1
print ('You can add maximum ten names')
b=print(input("Enter your name :"))
while True or i!=10:
        c=input('Do you wish write more name y/n?:')
        if c=='y' or i!=10:
                    b=input("Enter your name :")
                    a.append(b)
                    i=i+1    
        elif c=='n' or i!=10:
                    exit()
                    print(a)
if i==10:
                    print (a)
                    print('Thanks')
The or part of if c=='y' or i !=10: is resolving to true when c is not equal to "y"
arr = []
print('You can enter up to ten names')
while len(arr) <= 10: # maximum 10 names
    flag = input('continue? y / n\n') # loop end flag
    try: assert flag.lower() in ('y', 'n')
    except:
        print('incorrect input, please try again')
        continue
    if flag.lower() == 'y':
        name = input('enter your name: ')
        arr.append(name)
    else: # flag == 'n'
        print('Thanks')
        break