简体   繁体   中英

Comparing two numbers without loops or branching in Python

I have to compare two numbers and write greater, then smaller, but I don't know how to get smaller one. I currently have this code:

a = int(input())
b = int(input())
c = round(((a + b) + abs(a - b)) / 2)
x = "should be smaller one"
print("{0}\n{1}".format(c, x))

较小的是: c = round(((a + b) - abs(a - b)) / 2)

If I right understand you:

In [1]: a , b = 3, 4

In [2]: smaller = a if a < b else b

In [3]: smaller
Out[3]: 3

Here you can replace < to other operator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM