You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
818 B
31 lines
818 B
10 months ago
|
import fire
|
||
|
import random
|
||
|
|
||
|
|
||
|
class GuessingGame:
|
||
|
def __init__(self, max_number=100):
|
||
|
"""初始化数据"""
|
||
|
self.number = random.randint(1, max_number)
|
||
|
self.max_number = max_number
|
||
|
self.guesses = 0
|
||
|
|
||
|
def play(self):
|
||
|
"""猜数游戏"""
|
||
|
print(f"猜一个介于1和{self.max_number}之间的数。")
|
||
|
|
||
|
while True:
|
||
|
guess = int(input("你的猜测是:"))
|
||
|
self.guesses += 1
|
||
|
|
||
|
if guess < self.number:
|
||
|
print("太小了,请继续猜。")
|
||
|
elif guess > self.number:
|
||
|
print("太大了,请继续猜。")
|
||
|
else:
|
||
|
print(f"恭喜你,猜对了!你一共猜了{self.guesses}次。")
|
||
|
break
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
fire.Fire(GuessingGame)
|