+ 6
I want to change numbers in list
I have to change the no.8 in the code to 42 and whenever anyone inputs 8 the output should be 42. How can I do that in list? https://code.sololearn.com/cfRVwi635QIJ/?ref=app
7 Answers
+ 8
~|しゅらだ|🦋~ ,
i am not quite sure if I understood you correctly. a list seems not to be the preferred way. better to use a dictionairy, that has a mapping of a *key: value* pair.
if we use the key, we get the value as a result like this:
numbers = {5:5, 8:42, 2:2}
inp = int(input())
print(numbers.get(inp, inp))
input of 1 will return 1, input of 2 will return 2 and input of 8 will return 42 (or whatever value is mapped to the key of *8*)
+ 8
~|しゅらだ|🦋~ ,
i have the feeling that there is some information in your post missing. it would be great if you could provide a short description of what the code should achieve, what the input is, and what the output should looks like.
+ 7
But when I typed 8 the whole list is coming. I want only 42 to come instead of 8?
+ 6
~|しゅらだ|🦋~
You can use the `index()` method to find the index of the number 8 in the list and then replace it with 42.
Are you wanna this?
See this I modified your code..
https://code.sololearn.com/cpdO95M93mvM/?ref=app
Edit:-check again..
+ 5
Okay but if I have to choose both number 5 and 42 so when I do that in dictionary it said not found ?
+ 5
Ok thankyou !
+ 4
If you want to print 42 if the input is 8 then your logic is wrong. If you simply want to do that, then change the if logic to:
if q == 8:
print(42)
But, it means that your list will be unnecessary. I think you should edit your post and clearly describe what you want to do.