尝试创建一个随机数生成器,要求用户输入一个数字,并将其与生成的数字进行比较。如何使比较的数字不显示负数?你知道吗
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岁了?你知道吗
在f字符串中执行此操作的方式与在任何其他上下文中执行此操作的方式相同:通过调用^{} ,绝对值函数。你知道吗
相关问题 更多 >
编程相关推荐