|
|
发表于 2017-12-27 15:41:31
|
显示全部楼层
问题出现在
while type(temp) != type(1):
在Python中input返回的是字符串
下面是修改后的代码:
- #导入随机模块
- import random
- #设定循环次数
- times = 3
- #生成1-9的随机数
- secret = random.randint(1,10)
- print('------------------我爱鱼C工作室------------------')
- # 这里先给guess赋值(赋一个绝对不等于secret的值)
- guess = 0
- # print()默认是打印完字符串会自动添加一个换行符,end=" "参数告诉print()用空格代替换行
- # 嗯,小甲鱼觉得富有创意的你应该会尝试用 end="JJ"?
- print("不妨猜一下小甲鱼现在心里想的是哪个数字:", end=" ")
- while (guess != secret) and (times > 0):
- temp = input()
- while not temp.isdigit():
- print("请输入整数", end=' ')
- temp = input()
- guess = int(temp)
- times = times - 1 # 用户每输入一次,可用机会就-1
- if guess == secret:
- print("我草,你是小甲鱼心里的蛔虫吗?!")
- print("哼,猜中了也没有奖励!")
- else:
- if guess > secret:
- print("哥,大了大了~~~")
- else:
- print("嘿,小了,小了~~~")
- if times > 0:
- print("再试一次吧:", end=" ")
- else:
- print("机会用光咯T_T")
- print("游戏结束,不玩啦^_^")
复制代码 |
|