-2

i created an array of names. it allows user to add, if user says exit it stops the list and prints it. how do i stop it from putting the work EXIT into the list and get rid of the square brackets and speech marks. Thanks


import array
names = ['umayr', 'bob', 'harry']
print("Here is current names: " + str(names))

addname = ""
while addname != 'exit':
    addname = input("Enter a new name or exit:   ")
    names.append(addname)

if addname == 'exit':
    break

print('List of Names' + str(names))
DYZ
  • 55,249
  • 10
  • 64
  • 93
Agena
  • 1
  • 1
  • First, please fix the indentation. `break` is not allowed outside of a loop. Second, your question is about lists, not arrays. You do not need `import array` (you do not use it anyway). Please update the question. – DYZ Oct 14 '20 at 18:48
  • Please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results differ from what you expected. We should be able to copy and paste a contiguous block of your code, execute that file, and reproduce your problem along with tracing output for the problem points. This lets us test our suggestions against your test data and desired output. – Prune Oct 14 '20 at 18:49
  • "how do I stop it ... ?" You wrote the code: when you find `exit`, *don't put it in the list*. Your loop control is out of order: it has to be input, then test/break, then append. – Prune Oct 14 '20 at 18:52
  • "get rid of speech marks" ... In general, you don't -- not at this level of programming. The quotation marks are part of the language definition of a string value. You get rid of the brackets by printing the items individually, rather than as a list. – Prune Oct 14 '20 at 18:53
  • Could you give some feed-back? Did you look at the answers? – trincot Oct 18 '20 at 10:45

3 Answers3

0

You need to have your if addname == 'exit': before the append.

Like this

names = ['umayr', 'bob', 'harry']
print("Here is current names: " + str(names))
 
while True:  # You dont need to do the check twice
    addname = input("Enter a new name or exit:   ")
    if addname == 'exit':
        break
    names.append(addname)



print('List of Names' + str(names))
FrozenAra
  • 509
  • 2
  • 14
0

Instead of adding a condition in while loop, you can run an infinite loop using while True and when you take input from a user you can check if it exists you can break your loop

import array
names = ['umayr', 'bob', 'harry']
print("Here is current names: " + str(names))

addname = ""
while True:
    addname = input("Enter a new name or exit:   ")
    if addname == "exit":
        break
    names.append(addname)

print('List of Names' + str(names))
abdulsaboor
  • 678
  • 5
  • 10
0

To print without brackets and quotes, use:

", ".join(names)

For avoiding to add the EXIT, change the order of input & append and perform an initial input:

addname = input("Enter a new name or exit:   ")
while addname != 'exit':
    names.append(addname)
    addname = input("Enter a new name or exit:   ")
trincot
  • 317,000
  • 35
  • 244
  • 286