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
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
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
  format json outputs ! evilcode1 3 1,692 Oct-29-2023, 01:30 PM
Last Post: omemoe277
  Formatting outputs created with .join command klairel 2 594 Aug-23-2023, 08:52 AM
Last Post: perfringo
  I have written a program that outputs data based on GPS signal kalle 1 1,134 Jul-22-2022, 12:10 AM
Last Post: mcmxl22
  Why does absence of print command outputs quotes in function? Mark17 2 1,342 Jan-04-2022, 07:08 PM
Last Post: ndc85430
  Thoughts on interfacing with a QR code reader that outputs keystrokes? wrybread 1 1,449 Oct-08-2021, 03:44 PM
Last Post: bowlofred
  Combining outputs into a dataframe rybina 0 1,650 Mar-15-2021, 02:43 PM
Last Post: rybina
  Multi set string inputs/outputs kwmcgreal 2 2,010 Sep-26-2020, 10:44 PM
Last Post: kwmcgreal
  How to use subprocess to get multiple data outputs in desired folder? 3SG14 1 2,165 Sep-19-2020, 05:46 PM
Last Post: bowlofred
  Outputs missing SamAnw 4 2,540 Feb-12-2020, 04:32 PM
Last Post: adetheheat
  Interpreter and running a .py file give different outputs PythonNPC 5 2,946 Jul-21-2019, 01:07 PM
Last Post: PythonNPC

Forum Jump:

User Panel Messages

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