Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while loop
#1
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')
Yoriz write Aug-18-2021, 10:42 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
The or part of if c=='y' or i !=10: is resolving to true when c is not equal to "y"
Reply
#3
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
Reply


Forum Jump:

User Panel Messages

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