Posts: 2 Threads: 1 Joined: Jan 2021 Hi there, I face the problem that I have no idea yet, how to program this: pos0 = input() example: pos0 = 160 pos1 and pos2 should also get the same what is in pos0. => pos1 = 160 and pos2 = 160 What kind of method I can use to program this ? I really appreciate your answers Posts: 2,171 Threads: 12 Joined: May 2017 It looks silly, but works: pos0 = pos1 = pos2 = input() another way: pos0 = input() pos1 = pos0 pos2 = pos0 If you change the value of pos0, pos1 and pos2 won't change. Posts: 8,197 Threads: 162 Joined: Sep 2016 Jan-12-2021, 01:45 PM (This post was last modified: Jan-12-2021, 03:13 PM by buran.) Just to mention that, instead of creating multiple names (variables) like pos0, pos1, pos2... it may be better to use some sort of collection. It depends on what you want to do with pos0, pos1, pos2... Posts: 2 Threads: 1 Joined: Jan 2021 (Jan-12-2021, 01:11 PM)DeaD_EyE Wrote: It looks silly, but works: pos0 = pos1 = pos2 = input() another way: pos0 = input() pos1 = pos0 pos2 = pos0 If you change the value of pos0, pos1 and pos2 won't change. Think you just need to be a little careful - your alternative option works great for scalar variables but with lists, objects (and I think tuples as well) this will cause issues as this copies the position reference of the variable and not the value of the variable Kind regards RF Posts: 2,171 Threads: 12 Joined: May 2017 Jan-12-2021, 07:26 PM (This post was last modified: Jan-14-2021, 09:05 AM by DeaD_EyE.) For mutable objects, copy.copy and/or copy.deepcopy can be used. Lists, Dicts, Sets have a copy method. Lists can also copied with: import copy my_list_1 = [1, 2, 3] my_list_2 = my_list_1[:] my_list_3 = my_list_1.copy() my_list_4 = copy.copy(my_list_1) Lists can keep also lists and other objects. To copy them together with the nested content, copy.deepcopy should be used. import copy nested_list_1 = [1, 2, [3, 4, 5], 6] nested_list_2 = nested_list_1[:] # the inner list is not copied nested_list_3 = copy.deepcopy(nested_list_1) # inner list is also copied nested_nested_list_1 = [[[1]], [[2]]] nested_nested_list_2 = copy.deepcopy(nested_nested_list_1) # now changing the 1 to 66 nested_nested_list_2[0][0][0] = 66 # nested_nested_list_1 has the original value 1 # nested_nested_list_2 has instead 66 print(nested_nested_list_1) print(nested_nested_list_2) Docs for copy module: https://docs.python.org/3/library/copy.html Posts: 2 Threads: 1 Joined: Jan 2021 (Jan-12-2021, 01:11 PM)DeaD_EyE Wrote: It looks silly, but works: pos0 = pos1 = pos2 = input() another way: pos0 = input() pos1 = pos0 pos2 = pos0 If you change the value of pos0, pos1 and pos2 won't change. => thank you for your inspiration, I made it with a variable: var = input() pos0 = var pos1 = var pos2 = var Posts: 1,953 Threads: 8 Joined: Jun 2018 Jan-14-2021, 08:52 AM (This post was last modified: Jan-14-2021, 08:52 AM by perfringo.) (Jan-14-2021, 07:42 AM)Tricia279 Wrote: => thank you for your inspiration, I made it with a variable: var = input() pos0 = var pos1 = var pos2 = var  I hope that you do realize that this will not deliver result(s) you defined in your initial post. You wanted int but this will deliver str. To illustrate problem and provide alternative using dictionary: >>> initial = input('Enter initial value: ') Enter initial value: 160 >>> initial '160' >>> positions = dict.fromkeys((0, 1, 2), initial) # range(3) can be used instead of (0, 1, 2) >>> positions {0: '160', 1: '160', 2: '160'} >>> positions[0] '160' >>> positions[2] = 160 >>> positions {0: '160', 1: '160', 2: 160} I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame. |