python 练习题

猜数字游戏,通过系统生成一个随机数作为预设年龄,对用户提供3次猜的机会,前两次如果没有猜中给出数字范围大小的提示,如果第3次还没有猜中则给予鼓励提示,同时打印出这个预设的数字,当然三次当中有任何一次猜中会给用户猜中提示的:

 #!/usr/bin/env python
 2 #! -*- coding:utf-8 -*-
 3 import random #导入随机数模块
 4 Age=random.randrange(10)#随机生成一个小于10的整数(0-9,不包括负数),并赋值给Age
 6 for i in range(3):
 7     if i < 2:
 8         guess_number=int(input("Please input the age of my dog you guess:\n"))
 9         if guess_number > Age:
10             print("The age you guess is a little big, think smaller!\n")
11         elif guess_number < Age:
12             print("The age you guess is a little small, think bigger!\n")
13         else:
14             print("Bingo, you got the number,congratulations!\n")
15             break
16     else:
17         guess_number=int(input("Please input the age of my dog you guess:\n"))
18         if guess_number == Age:
19             print("Bingo, you got the number,congratulations!\n")
20         else:
21             print("Oh,you just got bad luck, come to try again, you can do it! The actual age of my dog is %d...\n"% Age)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值