简体   繁体   English

为什么 `n1 if eval(“n1<=n2”) else n2` 总是返回 `n1`?

[英]Why does `n1 if eval(“n1<=n2”) else n2` always return `n1`?

I'm trying to write a function smaller_num(n1, n2) which returns the smaller value of two parameters.我正在尝试编写一个 function small_num smaller_num(n1, n2) ,它返回两个参数的较小值。 In the snippet below, although I expect the function to return "16" , actually "1500" is returned for some reason.在下面的代码段中,虽然我希望 function 返回"16" ,但实际上由于某种原因返回了"1500" What's wrong with the logic?逻辑有什么问题?

def smaller_num(n1, n2):
    return  n1 if eval("n1<=n2") else n2
print(smaller_num("1500", "16"))

Your use of eval() is redundant.您对eval()的使用是多余的。 Try simply简单尝试

def smaller_num(n1, n2):
    return  n1 if n1<=n2 else n2

It's now clear that现在很清楚了

print(smaller_num("1500", "16"))

amounts to总数是

"1500"<="16"

ie you are really comparing strings.即你真的在比较字符串。 This is done lexicographically (ie according to their position in the ASCII/Unicode table), character by character.这是按字典顺序(即根据 ASCII/Unicode 表中的 position)逐个字符完成的。 So though the first character are the same ( "1" and "1" ), the second character ( "5" vs. "6" ) is not "smaller or equal" for the first variable.因此,尽管第一个字符相同( "1""1" ),但第二个字符( "5" vs. "6" )对于第一个变量来说不是“小于或等于”。 The remaining two characters ( "00" ) are disregarded, as the other string has run out of characters.剩下的两个字符( "00" )被忽略,因为另一个字符串已经用完了字符。 An equivalent comparison is then "15"<="16" , which also happens to be equivalent to 15<=16 as integers appear consecutively on the ASCII/Unicode table.一个等效的比较然后是"15"<="16" ,它也恰好等于15<=16因为整数连续出现在 ASCII/Unicode 表上。

To do what you want, just use做你想做的事,只需使用

print(smaller_num(1500, 16))

You may even keep your original eval() in smaller_num() , though I don't see the point.您甚至可以将原始eval()保留在smaller_num()中,尽管我不明白这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 n1 和 n2 之间存在差异? - Why there is a difference between n1 and n2? 适用于0 ^ n1 ^ n2 ^ n的Python图灵机 - Python turing machine for 0^n1^n2^n 确定 a、b 中是否存在数字 n1、n2 和 c 中的 n3,使得 n1 + n2 = n3 [ftt,多项式乘法] - Determining if there exists numbers n1, n2 in a, b and n3 in c such that n1 + n2 = n3 [ftt, polynomial multiplication] 将下面的赋值操作(n1,n2 = n2, n1+ n2 )拆分为 n2 = n1+n2 和 n1=n2 时,代码不能正常工作,为什么? - When splitting the assignment operation (n1,n2 = n2, n1+ n2 ) below, into n2 = n1+n2 and n1=n2 separately, the code doesn't work properly, why? 通过在python中使用super()。Sub(n1,n2)出错 - Error by using super().Sub(n1,n2) in python 转换后获取 \n1 \n2 输出 - Getting \n1 \n2 outputs after conversion 从包含 {(w1,n1),(w2,n2)} 形式的元组的字典中打印单词的频率以及类似 (W1: N1 ) (W2: N2) 的单词,其中 N1&gt;N2 - Print frequency of the words along with word like (W1 : N1 ) (W2 : N2) with N1>N2 from a dictionary containing tuple in form of {(w1,n1),(w2,n2)} 如何制作一个 function,它返回列表 n 中参数 n1 或 n2 的倍数的所有整数 - How to make a function that returns all integers that are multiples of either parameters n1 or n2 within the list n csv.writer - 如何在 writerow(n1,n2,n3, …nth) 中动态写入列? - csv.writer - How to dynamically write columns in writerow(n1,n2,n3, …nth)? 如何从 python 列表中的字符串中删除 \n1、\n2、\n3 等? - How to remove \n1, \n2, \n3 etc. from a string in python list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM