|
|
发表于 2017-7-4 16:02:25
|
显示全部楼层
- import random
- secret = random.randint(1,10)
- temp = input("不妨猜一下小甲鱼现在心里想的是哪个数字:")
- i = 3
- while guess != secret and i >= 2:#这里在没对guess赋值的时候就调用了guess,错了
- i = i-1 #上边提到
- while not temp.isdigit():
- temp = input("抱歉,您的输入有误,请输入一个整数:")
- guess = int(temp)
- if guess > secret:
- temp = input('猜大了,请重新输入:')
- guess = int(temp)
- else:
- temp = input('猜小了,请重新输入:')
- guess = int(temp)
- if guess == secret:#这里的缩进也不对,这样的话if和while属于同一级别,
- #那你每一次出现的数字都是随机的
- print("握草对了,你是我心里的蛆虫吗?")
- else:
- print("三次都没猜中,GG")
复制代码
我想了好长时间,最后还是用小甲鱼老师的方法做的
- import random
- secret = random.randint(1,10)
- times = 3
- guess = 0
- print("不妨猜一下小甲鱼现在心里想的是哪个数字:", end=" ")
- while (guess != secret) and (times > 0):
- temp = input()
- if temp.isdigit():
- guess = int(temp)
- if guess == secret:
- print("握草对了,你是我心里的蛆虫吗?")
- print("哼,猜中了也没有奖励!")
- else:
- if guess > secret:
- print("猜大了,请重新输入:")
- else:
- print("猜小了,请重新输入:")
- if times > 1:
- print("再试一次吧:", end='')
- else:
- print("机会用光咯T_T")
- else:
- print("抱歉,您的输入有误,请输入一个整数:", end='')
- times = times - 1 # 用户每输入一次,可用机会就-1
- print("游戏结束,不玩啦^_^")
复制代码
耽误你时间了,不好意思。。。 |
|