如何在用户猜测数字时在python上修复负数

时间:2018-09-24 03:49:47

标签: python

是否可以在下面的代码中修正获取负数的方法?

import random

number = random.randrange(1, 100)
guess = int(input("I’m thinking of a number between 1 and 100 " + "Can you guess it? "))

number1 = number - guess
number2 = (number) - guess

if guess>number:
    print ("You were " + str(number1)+ " away! ")
else:
    if guess<number:
        print ("You were " + str(number2)+ " away! ")
    else:
        if guess == number:
            print ("You got it right ")

2 个答案:

答案 0 :(得分:0)

state = { value:null } 将返回差的绝对值。

abs()

答案 1 :(得分:0)

整个代码应简化为:

flexDirection : 'row'

或者只是:

import random
number = random.randint(1, 100)
guess = int(input("Im thinking of a number between 1 and 100 Can you guess it? "))
if guess!=number:
    print ("You were",abs(guess-number),"away! ")
else:
    print ("You got it right ")

整个代码:

print(["You were "+str(abs(guess-number))+" away! ","You got it right "][number==guess])