python classic Guess

Original link: http://www.cnblogs.com/hiuhungwan/p/10549130.html
Title: Guess 
1. allows users to input 1-20 guessing, you can guess five times.
2. Each prompted, big or small!
3. If more than 5 times, suggesting that game over.

# !/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Hiuhung Wan
'''
题目:猜数字
1.让用户输入1-20,猜数字,可以猜5次。
2.每次有提示,大了,或者小了!
3.如果超过5次,提示game over.
'''
import random
init_number = random.randint(1, 20)   # 准确的数字
max_count = 5      # 最大猜的次数
count = 0          # 计数器

while count < max_count:
    try:
        temp = int(input("请输入一个整数(1-20):"))
        if temp == init_number:
            print("竟然猜对了!")
            break
        elif temp < init_number:
            print("小了点")
        else:
            print("大了点")
        count += 1
    except ValueError:
        print("给我个整数,谢谢")
else:
        print("猜对是不可能猜对的,这一辈子都不可能猜对的,数字是%s。" % init_number)

  

Results are as follows:

请输入一个整数(1-20):12.3
给我个整数,谢谢
请输入一个整数(1-20):12
竟然猜对了!

Process finished with exit code 0

  

请输入一个整数(1-20):12.3
给我个整数,谢谢
请输入一个整数(1-20):12
大了点
请输入一个整数(1-20):6
大了点
请输入一个整数(1-20):3
大了点
请输入一个整数(1-20):1
竟然猜对了!

Process finished with exit code 0

  

请输入一个整数(1-20):3
小了点
请输入一个整数(1-20):3
小了点
请输入一个整数(1-20):3
小了点
请输入一个整数(1-20):3
小了点
请输入一个整数(1-20):3
小了点
猜对是不可能猜对的,这一辈子都不可能猜对的,数字是17。

Process finished with exit code 0

  

Reproduced in: https: //www.cnblogs.com/hiuhungwan/p/10549130.html

Guess you like

Origin blog.csdn.net/weixin_30266885/article/details/94799179