基础版:
# -*- coding: utf-8 -*-
"""
功能:猜数游戏
作者:XxLyle
日期:2021.11.18
"""
import random
target = random.randint(0, 100)
x = int(input("Try to guess the number I'm thinking of:"))
while True:
if x > target:
x = int(input('Too high! Guess again:'))
elif x < target:
x = int(input('Too low! Guess again:'))
else:
break
print("That's it !")
print("Thanks for playing !")
改进版:
# -*- coding: utf-8 -*-
"""
功能:猜数游戏
作者:李学程
日期:2021.11.18
"""
import random
while True:
target = random.randint(0, 100)
x = int(input("Try to guess the number I'm thinking of:"))
while True:
if x > target:
x = int(input('Too high! Guess again:'))
elif x < target:
x = int(input('Too low! Guess again:'))
else:
break
choice = input("That's it ! Would you like to play again ?(yes / no)")
if choice == 'no':
break
print('Thanks for playing !')