如何删除f字符串表达式中的负号?

2024-09-29 23:16:46 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试创建一个随机数生成器,要求用户输入一个数字,并将其与生成的数字进行比较。如何使比较的数字不显示负数?你知道吗

    import random

number = random.randint(1, 100)
attempts = 1

print("I'm thinking of a number between 1 and 100.")
print("You only have one attempt.")

for tries in range(attempts):
    geuss = int(input("Geuss a number: "))

    if geuss != number:
        print(f"You were {geuss-number} away!")
    elif geuss == number:
        print(f"Congratulations! You geussed the number.")

假设随机生成的数字是63,用户输入25。当程序运行时,它会说你已经-38了!我怎么才能去掉负号让它说你38岁了?你知道吗


Tags: andof用户importyounumber数字random

热门问题