简体   繁体   中英

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). 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.

Also, I'm not sure if "elif Question >= "5000000" <= "9000000":" is correct. 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!"

You're comparing strings, when you should probably be comparing integers.

elif Question >= "5000000" <= "9000000": is incorrect.

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().

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

Then you can compare the output properly with integers.

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.

Ex. Program:

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

Program 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.

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

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