简体   繁体   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?")

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.

相关问题 Python | 硒| 继续输出页面源,而不是我手动检查元素时看到的内容 - Python | Selenium | Keeps outputting page source rather than what I see when I manually inspect element 了解为什么Bitcoinlib生成的地址与我拥有的地址不同 - Understand why Bitcoinlib is generating different addresses than what I have 我有一个代码,需要以不同的顺序打印出来,但它一直在括号中打印出来 - I have a code and need to print out in put in different order but it keeps printing it out in brackets 我正在努力弄清楚为什么我的代码不断输出错误的值 - I am struggling to figure out why my code keeps outputting the wrong values 为什么我的代码会打印一个我尚未分配的值? - Why does my code print a value that I have not assigned as yet? Python类,在通过print或format调用时具有与分配给value时不同的值 - Python class, have different value when called by print or format than when assigned to value 我有一个带有计分器的代码,该计分器不断重复 - I have a code that has a score counter that keeps repeating itself 编写第二个 function 后代码一直输出空白文本文件 - Code keeps outputting blank text file after writing second function 当我运行它时,我的Python代码不断返回不同的错误 - My Python code keeps returning different errors when i run it 为什么我在以下代码中使用的相同的Magic方法会生成与预期不同的输出 - Why is the same Magic method I used in the following code generating different output than what is expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM