繁体   English   中英

此代码不断输出与我分配的消息不同的消息

[英]This code keeps outputting different messages than what I have assigned

Question = input("How much money did Kingsman make this weekend?:")
if Question > "10000000":
    print("Wow! What an opening!")
elif Question >= "5000000" <= "9000000":
    print("Hey! Not a bad start!")
elif Question < "5000000":
    print("Are you happy with this result?")

如果我说 Kingsman 赚了 4,000,000 美元,它会输出电影票房超过 1000 万美元的信息,但真正奇怪的是,当我输入 $1 时,它会输出电影票房少于 500 万美元的信息(应该如此)和当我 go 高达 4,000,000 美元时,它显示 output 的价格不到 5 百万美元(应该如此)。 出于某种原因,相同数字的输出(在本例中为 400 万)从“哇。多么开放”到“你对这个结果是否满意”,我不明白为什么。

另外,我不确定"elif Question >= "5000000" <= "9000000":"是否正确。 我想说的是,当电影的收入在 500 万到 900 万之间时,它应该显示消息“嘿!不错的开始!”

当您可能应该比较整数时,您正在比较字符串。

elif Question >= "5000000" <= "9000000":不正确。

它应该是这样的:

elif 5000000 <= int(Question) <= 9000000:

您甚至可以通过将 int() 放在 input() 上,将输入转换为 integer 格式。

Question = int(input("How much money did Kingsman make this weekend?: "))

然后您可以将 output 与整数进行正确比较。

if Question > 10000000:

您的参数将不再有问题。 您的程序响应不同的原因是因为您将字符串输入与其他字符串输入进行比较。 它仍然进行比较而没有抛出任何错误的原因是因为它的 unicode 值。

前任。 程序:

print('apple' == 'Apple')
print('apple' > 'Apple')
print('A unicode is', ord('A'), ',a unicode is', ord('a'))

程序 Output:

False
True
A unicode is 65 ,a unicode is 97

这只是为了参考为什么它允许您比较 2 个字符串。

https://www.journaldev.com/23511/python-string-comparison

暂无
暂无

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

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