Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ONE input => THREE outputs
#1
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
Huh
Reply
#2
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.
buran likes this post
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
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...
ndc85430 likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(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
Reply
#5
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
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
(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
Smile
Reply
#7
(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
Smile

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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Multi set string inputs/outputs kwmcgreal 2 364 Sep-26-2020, 10:44 PM
Last Post: kwmcgreal
  How to use subprocess to get multiple data outputs in desired folder? 3SG14 1 437 Sep-19-2020, 05:46 PM
Last Post: bowlofred
  Outputs missing SamAnw 4 585 Feb-12-2020, 04:32 PM
Last Post: adetheheat
  Interpreter and running a .py file give different outputs PythonNPC 5 876 Jul-21-2019, 01:07 PM
Last Post: PythonNPC
  Save outputs to Desktop cisicomania 1 1,080 Aug-31-2018, 10:08 AM
Last Post: Gribouillis
  Help Editing/Problem with outputs stanthaman42 7 1,743 Jul-11-2018, 05:30 PM
Last Post: ichabod801
  how do i rewrite this code to give me 10 outputs BlackPimpernel 2 1,139 Mar-29-2018, 11:29 AM
Last Post: BlackPimpernel
  Different outputs in Python2 and Python3 MikeHill 3 2,127 Jun-07-2017, 08:58 PM
Last Post: Ofnuts
  Program that outputs HQ addresses of companies from Google + Local pages frenchgirl1309 2 2,399 Nov-14-2016, 10:11 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020