0

Почему вывод 7?

Я бросил вызов другому пользователю, и там была такая задача(см. ниже), вывод которой было число 7 x = 5 x, x = x + 1, x + 2 print(x) Я не могу понять, почему ответом будет число 7? Разве тут не используется одна переменная?

8th Oct 2023, 12:31 PM
Buterbrod:)
Buterbrod:) - avatar
3 Réponses
0
Sakshi, I don't think so. I move the code around and get a different result. x = 5 x, x = x+2, x+1 print(x), and 6 is printed on screen My theory is computer read code from left to right. x, x + = x+2, x+1 will be translated to x, x = 7, 6 x first was reassigned to 7, and then reassigned to 6. This problem is similar to another question few days ago. https://www.sololearn.com/discuss/3244848/?ref=app In that question if we follow "from left to right" method, we would expect [2, 3, 1, 4] as the result. In that post I made a theory. If a and b are being reassigned, and b is referencing a, a will get reassigned first, and b will reference to new a before reassign. Now seeing this question make my theory not entirely correct. Now I got a new theory to that question.
9th Oct 2023, 4:10 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 3
x = 5 x,x = x+1, x+2 That means: x,x = 5+1, 5+2 (which is 6 and 7) And I think 7 is print because this is maximum number so, that's why is print
8th Oct 2023, 4:43 PM
Sakshi [Offline 🙃]
Sakshi [Offline 🙃] - avatar
0
x, x = x+1, x+2 – это ни что иное как множественное присваивание. То есть в конечном итоге мы присвоили x значение 7 и вывели на печать: x = 5 x = 6 x = 7 print(x) Другими словами это распаковка кортежа: x, x = (6, 7) – скобки не обязательны Вы можете проделать это с множеством переменных...😎
8th Oct 2023, 5:03 PM
Solo
Solo - avatar