|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这个怎么原来的代码里面让他进行下去啊??为啥我插进去不管输入啥都是不合法??
- #这是原代码
- import random
- times = 3
- secret = random.randint(1,10)
- print('------------------我爱鱼C工作室------------------')
- # 这里先给guess赋值(赋一个绝对不等于secret的值)
- guess = 0
- # print()默认是打印完字符串会自动添加一个换行符,end=" "参数告诉print()用空格代替换行
- # 嗯,小甲鱼觉得富有创意的你应该会尝试用 end="JJ"?
- print("不妨猜一下小甲鱼现在心里想的是哪个数字:")
- while (guess != secret) and (times > 0):
- 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("再试一次吧:")
- else:
- print("机会用光咯T_T")
- print("游戏结束,不玩啦^_^")
复制代码
- #这是要插入的
- temp = input("不妨猜一下小甲鱼现在心里想的是哪个数字:")
- # not操作符的作用是将布尔类型的结果翻转:即取反的意思,not True == Flase
- while not isinstance(temp, int):
- print("抱歉,输入不合法,", end='')
- temp = input("请输入一个整数:")
复制代码
感觉你想插入的那个代码只是一个示例而已,怎么看都在这里用不上,input() 获取到的一定是一个 str 类型的,你后面再怎么用 isinstance(temp, int) 返回的都是 False
也就是说无论你输入的是什么,你都会是不合法的
这是我学这一块的时候写的
- import random
- print("---------- This is my first try -----------")
- number = random.randint(0, 19) # get a random number from [0,19]
- times = 0 # how many chance the player already used
- chance = 5 # how many chance the player have
- while(times < chance):
- temp = input("enter a number here!\n")
- if temp.isdigit():
- times += 1
- guess = int(temp)
- if(guess == number):
- print("yes, you are right!\nCongradulations, you win!")
- break
- else:
- if(times < chance):
- print("please try again!")
- if(guess < number):
- print("it is too small!")
- elif(guess > number):
- print("it is too big!")
- print("chance tried: " + str(times) + "/" + str(chance))
- else:
- print("you run out of the chance!\ngame is over!")
复制代码
|
|