#can someone help me and tell me why my code isn't working as it should?
22 messages · Page 1 of 1 (latest)
Thanks for your question :clap:, if someone gives you an answer it would be great if you thanked them with a :white_check_mark: in response. This response will earn you both points for special roles on this server.
What is your input? What are you trying to do that does not work. According to the name of your Function it works as intended
Well it basiccally does correctly what you want it to do it changes the index of 0 which is the character '1' to '6' and then the Output is correct. What is the desired output?
So if you ask yourself why it also adds the comas (,) and the spaces ( ' ' ) then the Issue is, that the input is a String, a String is an array of Characters (basically a List of Characters). So now if you convert the String to an List/Array it will just split it in Characters which includes the spaces and the comas. If you only want numbers in your list you only need to add numbers without spaces or comas in your input:
123
0
6
output:
['6', '2', '3']
If I got it wrong and something else is your problem, please let me know what output is desired. Or if you need further explanation let me know what you cant understand, and I'll give my best to explain it to you.
@lucid snow
"if you only want numbers in your list you only need to add numbers without spaces or commas in your input" that's exactly what i did brother
and it still gave a weird output
i want study coding
Your sample input was:
1, 2, 3
There are commas and a space in it.
oh yeah
what if i remove the data type for lst
i'd put
lst = input() instead
of
lst = list(input())
then lst will be: "1,2, 3"
Just a string with the given input. the Index of 0 will still be the first character. Because as I said a String is still a list of Characters.
So your Output should be:
6,2, 3
so if you want your list to be [1, 2, 3] then your input should be: 123
Without any comma or space.
So a way would be that the functions filters out the numbers and adds them to a list, after that the code is the same. Kind of like this:
def change_element(lst, index, new_element):
lst = [number for number in lst if number.isdigit()]
lst[index] = new_element
return lst
x = input("Enter your List: ")
y = int(input("Enter the index to Change: "))
z = input("Enter the new Element: ")
print(change_element(x, y, z))
here the Execution:
Enter your List: [1, 2, 3]
Enter the index to Change: 0
Enter the new Element: 9
['9', '2', '3']
Does this help you? @lucid snow