我敢肯定,这已经被问过了,但是我很难找到一个简单明了的答案。有什么建议么?
我的代码如下所示:

if int(guess1 == int(answer + 1) and guess2 == int(answer + 2))
    print("The answer was: " + answer + ". Player 1 wins!")
if int(guess1 == int(answer + 1) and guess2 == int(answer - 2))
        print("The answer was: " + answer + ". Player 1 wins!")
if int(guess1 == int(answer - 1) and guess2 == int(answer + 2))
    print("The answer was: " + answer + ". Player 1 wins!")
if int(guess1 == int(answer - 1) and guess2 == int(answer - 2))
        print("The answer was: " + answer + ". Player 1 wins!")


如果看起来更像这样,那就太好了:

if int(guess1 == int(answer ± 1) and guess2 == int(answer ± 2))
            print("The answer was: " + answer + ". Player 1 wins!")

最佳答案

根据您的评论:


  但是,关键是要找出两个猜测中哪个最接近答案。


在这种情况下,解决方案将变成:

if guess1 == guess2::
    print("It's a tie. The answer was {}".format(answer)
elif abs(guess1 - answer) < abs(guess2 - answer):
    print("Player 1 wins. The answer was {}".format(answer)
else:
    print("Player 2 wins. The answer was {}".format(answer)

关于python - 有没有办法在python中包含±(正负)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59959878/

10-12 20:58