Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Trouble Understanding Why This Code Works crocolicious Unladen Swallow Posts: 3 Threads: 2 Joined: Apr 2019 Reputation: 5 Likes received: 1 #1 Apr-05-2019, 02:48 PM (This post was last modified: Apr-05-2019, 03:24 PM by Yoriz. Edited 2 times in total. Edit Reason: Fixed code tags ) 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 Yoriz wrote Apr-05-2019, 03:24 PM:Great first post, i had to fix the python tags `/` was missing but you tried and all the info is there. well done Yoriz likes this post Yoriz Posts: 1,148 Threads: 28 Joined: Sep 2016 Reputation: 99 Likes received: 259 #2 Apr-05-2019, 03:14 PM 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)` crocolicious Unladen Swallow Posts: 3 Threads: 2 Joined: Apr 2019 Reputation: 5 Likes received: 1 #3 Apr-09-2019, 05:24 PM Thank you Yoriz for your help! « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post yet another code that works on 2 but not in 3 rick666 1 122 Dec-12-2019, 08:12 PM Last Post: Gribouillis Code that works on py2 and not in py3 rick666 3 247 Nov-29-2019, 01:37 PM Last Post: rick666 Simple code works in Jupyter but not VS Code Matt_O 2 148 Nov-17-2019, 01:15 AM Last Post: Matt_O code works at command line but not in .py ridgerunnersjw 4 222 Sep-12-2019, 11:09 PM Last Post: ridgerunnersjw Everything works except for one line of code 357mag 3 209 Sep-06-2019, 06:03 PM Last Post: 357mag Code works in IDLE, appears to work in CMD, but won't produce files in CMD/Windows ChrisPy33 3 605 Jun-12-2019, 05:56 AM Last Post: ChrisPy33 Help Understanding Code Variables 1 295 May-02-2019, 05:53 PM Last Post: micseydel Need help understanding simple Array code. Please. stluwa 1 336 Apr-13-2019, 07:16 PM Last Post: loomski Catching exceptions in embedded code no longer works on 3.7.2? FFMG 5 450 Feb-02-2019, 10:15 AM Last Post: Larz60+ Help Understanding Portion of Code caroline_d_124 3 444 Jan-15-2019, 12:12 AM Last Post: caroline_d_124

Forum Jump:

Users browsing this thread: 1 Guest(s)