ONE input => THREE outputs - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: ONE input => THREE outputs (/thread-31963.html) |
ONE input => THREE outputs - Tricia279 - Jan-12-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 RE: ONE input => THREE outputs - DeaD_EyE - Jan-12-2021 It looks silly, but works: pos0 = pos1 = pos2 = input()another way: pos0 = input() pos1 = pos0 pos2 = pos0If you change the value of pos0, pos1 and pos2 won't change. RE: ONE input => THREE outputs - buran - Jan-12-2021 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... RE: ONE input => THREE outputs - RubenF85 - Jan-12-2021 (Jan-12-2021, 01:11 PM)DeaD_EyE Wrote: It looks silly, but works: 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 RE: ONE input => THREE outputs - DeaD_EyE - Jan-12-2021 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 RE: ONE input => THREE outputs - Tricia279 - Jan-14-2021 (Jan-12-2021, 01:11 PM)DeaD_EyE Wrote: It looks silly, but works: => thank you for your inspiration, I made it with a variable: var = input() pos0 = var pos1 = var pos2 = var RE: ONE input => THREE outputs - perfringo - Jan-14-2021 (Jan-14-2021, 07:42 AM)Tricia279 Wrote: => thank you for your inspiration, I made it with a variable: 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} |