当前位置: 动力学知识库 > 问答 > 编程问答 >

Python: If a == b overrides a < b and a > b

问题描述:

I have made a simple dice rolling game. You start with 5 dice and the values of the 5 dice earn you a score. You then get the chance to 're-roll' the dice which produces different values and a new score.

To figure out your highest score for the round I have made a few if statements. The only problem is that every result whether the scores be higher, same or lower still produce the "if a == b".

What do I do?

if giveResult(dice) > giveResult(newDice):

print("Your first score was higher than your re-rolled dice score...")

print("Your highest score this round is: ", giveResult(dice))

elif giveResult(dice) == giveResult(newDice):

print("Your first score and re-rolled dice score are the same...")

print("Your highest score this round is: ", giveResult(dice))

elif giveResult(newDice) > giveResult(dice):

print("Your re-rolled dice give you a higher score... ")

print("Your highest score this round is: ", giveResult(newDice))

网友答案:

It's fair to assume that the == in python does exactly what it is designed to do. If there were a bug, it would have been found long ago. So, if python is telling you giveResult(dice) is equal to giveResult(newDice), it must be true.

The way you can debug this is to not call giveResult in the if statement. Instead, call it once and save the value. Then, you can print out the value before doing the test.

For example

r1 = giveResult(dice)
r2 = giveResult(newDice)
print("r1:", r1, "r2:", r2)
if r1 > r2:
    ...
elif r1 == r2:
    ...
elif r2 > r1:
    ...
网友答案:

It's almost certain that giveResult() returns the same value for dice and newDice. It could be because you've forgotten a return statement (so the function is implicitly returning None) or for some other reason -- it's impossible to say without seeing the function's source code.

there are 2 giveResult functions...

In Python, you can't have two functions with the same name. If you try and define two, the second would overwrite the first. That could well be part of your problem (again, it's impossible to be sure without seeing the source code).

分享给朋友:
您可能感兴趣的文章:
随机阅读: