随机生成一个正整数(取值范围[1,100])。让用户猜数字,并给出相应的提示:如果用户输入比答案大,提示‘Too big, try again’;反之,提示‘Too small, try again’;如果猜中了,提示‘Congratulations!’。最后,要给出反馈(答案,猜的次数,猜的历史)。

用python编程一个猜数字的小游戏
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
3条回答 默认 最新
关注
import random n = random.randint(1,100) count = 0 history = [] while True: m = int(input("Please input your guess between 1 and 100, inclusively")) count += 1 history.append(m) if m==n: print('Congratulations!') print('answer: %d' % m) break elif m>n: print('Too big, try again') else: print('Too small, try again') print('statistics: %d times' % count) print('history:', history)
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用