Python Forum
Trouble Understanding Why This Code Works
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble Understanding Why This Code Works
#1
Hello Community,
I'm studying a practice exam and the following question does not make sense to me. The answers have been provided but I'm still unsure how this answer was arrived at.

Which line can be used instead of the comment to cause the snippet below to produce the following expected output? (Select all that apply)

Code:
z, y, x = 2, 1, 0 
x, z = z, y
y = y - z 

# put line here 

print(x, y, z)
Expected output: 0, 1, 2

The available choices are below. The answer key says that both A and B answers are correct but I don't understand the logic in how they're both right. I tested both of them in a real environment (Python 3.7 using Spyder IDE) and they do work to produce the expected output. Can someone please help me understand WHY they both work? Many thanks in advance for your help. It is greatly appreciated!
A. x, y, z = y, z, x
B. z, y, x = x, z, y
C. y, z, x = x, y, z
D. The code is erroneous
Thanks again to the Community,
crocolicious
Reply
#2
After running
z, y, x = 2, 1, 0 
x, z = z, y
y = y - z 

print(x, y, z)
you can realise that x = 2, y = 0 & z = 1

with the variables that are being assigned to in the same order as the final print, the variables on the right are arranged to give correct order
x, y, z = y, z, x # same as x, y, z = 0, 1, 2
with with the variables that are being assigned to in the reverse order of the final print, the variables on the right are also arrange to give the reverse order.
z, y, x = x, z, y # same as z, y, x = 2, 1, 0
then the print is in the order of x, y, z
print(x, y, z)
Reply
#3
Thank you Yoriz for your help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor jst 4 875 Nov-15-2023, 06:56 PM
Last Post: jst
  Code understanding: Need help in understanding dictionary code jt123 0 450 Jul-09-2023, 01:13 PM
Last Post: jt123
  Code works but doesn't give the right results colin_dent 2 674 Jun-22-2023, 06:04 PM
Last Post: jefsummers
  New to python/coding Need help on Understanding why this code isn't working. Thanks! mat3372 8 1,662 May-09-2023, 08:47 AM
Last Post: buran
  Code used to work 100%, now sometimes works! muzicman0 5 1,383 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  Understanding a piece of code Michael1 4 1,372 Jan-20-2022, 07:14 PM
Last Post: Michael1
  Pyspark - my code works but I want to make it better Kevin 1 1,745 Dec-01-2021, 05:04 AM
Last Post: Kevin
Question email code works in 2.7 but not in 3 micksulley 3 2,537 Nov-04-2021, 09:44 PM
Last Post: micksulley
  My simple code don't works !! Nabi666 1 1,576 Sep-06-2021, 12:10 PM
Last Post: jefsummers
  HackerRank Problem: Code works on VS Code but not on the HackerRank site Pnerd 3 2,594 Feb-28-2021, 07:12 PM
Last Post: Pnerd

Forum Jump:

User Panel Messages

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