1

I'm trying to give the user of my "Terminal" a list from which they can choose what to do. But when I run my program, it includes the brackets as well. It still runs as normal, but it just looks a bit funny:

user_want_to_do = ["Open Google", "Access Files", "Exit Terminal"]
print("\n")
print(user_want_to_do)
opt = input("Please input what you want to do from the list above:")

Any Suggestions?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • So do you expect the user to type (without any mistake) the exact sentence they want to choose? Doesn't seem user friendly if you ask me. Why not ask them to type a single character (digit?) that identifies the choice? – trincot Jul 10 '23 at 10:15
  • Like this: `for e in user_want_to_do: print(e)`? – eyllanesc Jul 10 '23 at 10:17
  • Have a look at this: https://stackoverflow.com/questions/37565793/how-to-let-the-user-select-an-input-from-a-finite-list – Meiswjn Jul 10 '23 at 10:18
  • If you don't want the square brackets, then don't try to print a list directly. Print strings instead. – Tom Karzes Jul 10 '23 at 10:18
  • I could... but you don't see Windows asking you to type 1 for Files, 2 for Google... It just doesn't seen right. But I see where you're coming from:) – F4TAL_3RR0R.exe Jul 10 '23 at 10:18
  • You haven't specified how you want it to look... – alec_djinn Jul 10 '23 at 11:20

2 Answers2

3

You can just print them as strings by joining them

" ".join(user_want_to_do)

A "prettier" solution -

user_want_to_do = ["Open Google", "Access Files", "Exit Terminal"]

for i, item in enumerate(user_want_to_do, start=1):
    print(f"{i}. {item}")

Prints the list as a numbered list

Gopick
  • 61
  • 5
1

You can print without the brackets with unpacking:

user_want_to_do = ["Open Google", "Access Files", "Exit Terminal"]

print(*user_want_to_do, sep=", ") # removes quotes as well

output:

Open Google, Access Files, Exit Terminal

If you want them on separate lines instead:

print(*user_want_to_do, sep="\n") 

output:

Open Google
Access Files
Exit Terminal

If you want to keep the quotes you can map the strings to the repr function:

print(*map(repr,user_want_to_do), sep=", ")

output:

'Open Google', 'Access Files', 'Exit Terminal'

or strip brackets from the list's representation:

print(repr(user_want_to_do).strip('[]'))

output:

'Open Google', 'Access Files', 'Exit Terminal'
Alain T.
  • 40,517
  • 4
  • 31
  • 51
  • Code doesn't run; it just throws up an error message on "Google". – F4TAL_3RR0R.exe Jul 17 '23 at 10:24
  • Code only has one line and works on my computer. Did you try to run the sample output ? (I now separated the output in my answer to avoid confusion) – Alain T. Jul 17 '23 at 12:38
  • That does work, yes, but part of the issue is the fact that the speech marks are still included. – F4TAL_3RR0R.exe Jul 18 '23 at 11:10
  • What do you mean by "speech marks" ? If you're referring to quotes, two of the four solutions I proposed remove them. If not, you should include the exact output you're looking for in your question. – Alain T. Jul 18 '23 at 12:38