[英]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?")
If I was to say that kingsman made $4,000,000 it outputs the message for when the movie makes over 10mil, but the really weird thing is that when I input $1 it outputs the message for when the movie makes less than 5mil (as it should) and as I go up to $4,000,000 it displays the output for less than 5mil (as it should).如果我说 Kingsman 赚了 4,000,000 美元,它会输出电影票房超过 1000 万美元的信息,但真正奇怪的是,当我输入 $1 时,它会输出电影票房少于 500 万美元的信息(应该如此)和当我 go 高达 4,000,000 美元时,它显示 output 的价格不到 5 百万美元(应该如此)。 For some reason, the outputs for the same numbers are changing (in this case 4mil) from "wow. what an opening" to "are you happy with this result" and I don't understand why.出于某种原因,相同数字的输出(在本例中为 400 万)从“哇。多么开放”到“你对这个结果是否满意”,我不明白为什么。
Also, I'm not sure if "elif Question >= "5000000" <= "9000000":" is correct.另外,我不确定"elif Question >= "5000000" <= "9000000":"是否正确。 I'm trying to say that when the movie's revenue is in the range of 5mil to 9mil that it should display the message "Hey! Not a bad start!"我想说的是,当电影的收入在 500 万到 900 万之间时,它应该显示消息“嘿!不错的开始!”
You're comparing strings, when you should probably be comparing integers.当您可能应该比较整数时,您正在比较字符串。
elif Question >= "5000000" <= "9000000":
is incorrect. elif Question >= "5000000" <= "9000000":
不正确。
it should be something like:它应该是这样的:
elif 5000000 <= int(Question) <= 9000000:
You can even just convert the input into an integer format by putting the int() over the input().您甚至可以通过将 int() 放在 input() 上,将输入转换为 integer 格式。
Question = int(input("How much money did Kingsman make this weekend?: "))
Then you can compare the output properly with integers.然后您可以将 output 与整数进行正确比较。
if Question > 10000000:
You won't have an issue with your parameters anymore.您的参数将不再有问题。 The reason your program would respond differently because you were comparing string input with other string input.您的程序响应不同的原因是因为您将字符串输入与其他字符串输入进行比较。 The reason it still did the comparison without throwing any errors is because of its unicode value.它仍然进行比较而没有抛出任何错误的原因是因为它的 unicode 值。
Ex.前任。 Program:程序:
print('apple' == 'Apple')
print('apple' > 'Apple')
print('A unicode is', ord('A'), ',a unicode is', ord('a'))
Program Output:程序 Output:
False
True
A unicode is 65 ,a unicode is 97
This is just for reference to why it allowed you to compare 2 strings.这只是为了参考为什么它允许您比较 2 个字符串。
https://www.journaldev.com/23511/python-string-comparison https://www.journaldev.com/23511/python-string-comparison
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.