鱼C论坛

 找回密码
 立即注册
查看: 2189|回复: 2

[技术交流] (第三讲)改进猜数字的游戏-新手求点评

[复制链接]
发表于 2017-1-3 20:29:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
改进猜数字的游戏:
1.增加提示,猜的数字是大了还是小了
2.用户可以有3此机会
3.用户要猜的数字不能是固定不变得
===========================================================
import random
print ('--------Guess the number i set--------')
num = random.randint(1,10)

for i in range (0,2 +1):
    print ('Please input the number:')
    tem = input()
    guess = int(tem)
    if guess != num:
        if guess < num:
            print('Input number is low,input again!')
        else:
            print('Input number is big,input again!')
        print('Input wrong,you got three chances totaly.')
    else:
        print ('You get the correct number\n')
        print ('no any award though you correct')
        break
print ('Game over!^-^')


================= RESTART: E:\Python\练习题\guess the number.py =================
--------Guess the number i set--------
Please input the number:
7
You get the correct number

no any award though you correct
Game over!^-^
>>>
================= RESTART: E:\Python\练习题\guess the number.py =================
--------Guess the number i set--------
Please input the number:
3
Input wrong,you got three chances totaly.
Input number is low,input again!
Please input the number:
6
You get the correct number

no any award though you correct
Game over!^-^
>>>
================= RESTART: E:\Python\练习题\guess the number.py =================
--------Guess the number i set--------
Please input the number:
5
Input wrong,you got three chances totaly.
Input number is low,input again!
Please input the number:
9
Input wrong,you got three chances totaly.
Input number is low,input again!
Please input the number:
7
Input wrong,you got three chances totaly.
Input number is low,input again!
Game over!^-^
>>>
================= RESTART: E:\Python\练习题\guess the number.py =================
--------Guess the number i set--------
Please input the number:
1
Input number is low,input again!
Input wrong,you got three chances totaly.
Please input the number:
2
Input number is low,input again!
Input wrong,you got three chances totaly.
Please input the number:
3
Input number is low,input again!
Input wrong,you got three chances totaly.
Game over!^-^
>>>

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-1-3 20:32:26 | 显示全部楼层
tem = input()
     guess = int(tem)

input() 默认传递的字符串,这个地方有点老火,一定要强制转换下,否则在if条件比对时候会报错,不知大家有没有其他的办法可以解决的?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-9 20:33:07 | 显示全部楼层
改进了下,这个看起来舒服多了
import random
print("You get three times to input a number for the guess play")
answer = random.randint(0, 10)
times = 3
judge = True
while judge:
    guess = int(input("please input a number:"))
    if answer == guess:
        print("Great,You get the answer")
        print("There is no award though you get the correct answer")
        break
    else:
        times = (times - 1)
        if answer > guess:
            print("Your input bigger than the answer")
            print("you have %d to input" % times)
        else:
            print("Your input smaller than the answer")
            print("you have %d times to input" % times)
        if times == 0:
            judge = False
print("Game over.")
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 18:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表