我刚开始自学Python,我在用户输入的基础上错误地进行了排序活动。
这是代码:
a = int(input("Enter Num1:"))
b = int(input("Enter Num2:"))
c = int(input("Enter Num3:"))
d = int(input("Enter Num4:"))
if a > b: b, a = a, b
if b > c: c, b = b, c
if c > d: d, c = c, d
if a > b: b, a = a, b
if b > c: c, b = b, c
if a > b: b, a = a, b
print(d, c, b, a)我对if-else statement有一些基本的知识,但是我对这行if a > b: b, a = a, b中发生的事情一无所知。请解释给我听。谢谢
发布于 2020-11-25 08:28:07
它是一个交换操作,将a赋值给b,反之亦然。
a, b = 3, 4 # assign 3 to a, 4 to b
a, b = b, a
print(a, b) # prints "4 3"发布于 2020-11-25 08:28:30
x, y是分解的元组,其中x是有序对的第一个元素,y是第二个元组。=是赋值运算符。因此,基本上b, a = a, b交换值:b变成a,a变成b。
pair = (1, 2)
(x, y) = pair
x, y = pair # you can omit parenthesis
x, y = y, x # assign new valueshttps://stackoverflow.com/questions/65000931
复制相似问题