Python Forum
while loop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: while loop (/thread-34664.html)



while loop - mazeemagha - Aug-18-2021

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')



RE: while loop - Yoriz - Aug-18-2021

The or part of if c=='y' or i !=10: is resolving to true when c is not equal to "y"


RE: while loop - naughtyCat - Aug-28-2021

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