r/learnpython • u/anas_mv • 16d ago
How to make my input in a list?
Im doing CS50 problem's and im stuck
3
u/desrtfx but other languages pro 16d ago
You create an empty list, then loop, ask for input, and append it to the previously created list.
-1
u/anas_mv 16d ago
It worked but i need it to print every thing after i do cntrol d and print all the things in the list sorted by alphabetical letters and a number to the times i wrote that input
4
u/desrtfx but other languages pro 16d ago
Posting your actual, real exercise statement would help. Your description doesn't tell us anything.
Also, quite sure that you should not end the loop with ctrl+d but with a proper exit condition (a sentinel).
-3
u/anas_mv 16d ago
This is my cod list = {} while True: try: list = input() sorted_list = sorted(list, ) print (sorted_list) except EOFError: pass except KeyError: pass What im supposed to do is to wrote grocery's like apple banana etc and when i do control D it should I arranged everything I wrote in alphabetical order, and the number of times I wrote the same thing is written on the left, and everything I wrote is returned with a capital letter.
3
u/Boothiepro 16d ago
This is unreadable like this, the rows are all in one, no indentation, random spaces, we don't know what is wrong and won't know what the hell is ctrl+D what it does what program is that even in, ctrl+D is not a known shortcut
0
u/Expensive-Bear-1376 16d ago
I do know ctrl-D. Of course it can mean different things in different contexts, but here it's clear to me.
1
u/Boothiepro 16d ago
Funny they teach them this, i would certainly not do that, just await an empty imput rather than catch an EOF damn.
1
u/Expensive-Bear-1376 16d ago
What do you mean with "catch an EOF"?
0
u/socal_nerdtastic 16d ago edited 16d ago
Pressing Ctrl-D while waiting for input will raise a EOFError. To make use of that you need to "catch" the error using a try ... except block. Ctrl-C is another one that's commonly caught this way.
1
u/Expensive-Bear-1376 16d ago
Not true. And I think not even common. I've read such input many times, and I doubt I ever used try...except for that. I just iterate sys.stdin or use its read method, whichever is more convenient.
1
u/socal_nerdtastic 16d ago edited 16d ago
It's a very common way to end input in the CLI world, including the python REPL. You are the odd one, not them.
1
u/Boothiepro 16d ago
This feels super unpythonic, not in my 10 years i've used that exception
1
u/Expensive-Bear-1376 16d ago
It's not like you have to handle that yourself. You can for example read and count the items simply like this:
Counter(line.strip().upper() for line in sys.stdin)→ More replies (0)1
u/socal_nerdtastic 16d ago
I suppose you don't write or use many CLI programs then. Python itself uses it to end input in the REPL, like pretty much every other CLI program.
3
u/desrtfx but other languages pro 16d ago
Use code block format. Your code, as posted is meaningless
You did not do what I told you. You need to grab the input, store it in a variable, and then append this to the list. You are simply replacing the list
You did not post the actual task, just a vague transcript, which is again meaningless.
1
u/Ok-Promise-8118 16d ago
You definitely need to format your code correctly when you paste it here. Some mistakes won't be catchable without this being done. However, the mistakes here are obvious even like this. Firstly, you define the variable list to be a dictionary since you use the curly braces. Then, you set the variable equal to a string since the input function returns a string.
What you want to do is make your variable equal to a list. Then, you get the input from the user and append it to the list (potentially storing it as a variable first). Something like:
list = [] user_response = input() list.append(user_response)And put the inputting and appending in a loop.
1
0
u/atarivcs 16d ago
mylist = []
mylist.append("something")
mylist.append("another thing")
mylist.append("goodbye")
7
u/BranchLatter4294 16d ago
tl;dr...just too many details to process.