|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
0.让玩家输入1-10的整数,输入数据类型错误时让玩家重新输入
不知道哪里出错了,希望大佬纠正教学一下(新手,尽量使用通俗语言进行讲解谢谢)
- import random
- answers = random.randint(1,10)
- times = 1
- guess = 0
- while (guess != answers) and (times < 3):
- temp = input("从1-10选一个数:")
- if temp.isdight():
- guess = int (temp)
- if guess > answers:
- print("大了一丢丢呀,再来一次!")
- else:
- print("小了一点点呢,再试试!")
- temp = input("再猜一下:")
- if (times <= 3) and (guess == answers):
- print("聪明,这都给你猜中啦")
- else:
- print("三次机会到啦,下次再玩吧")
- else:
- print("不听话,要输入数字:")
- times = times + 1
-
- print("游戏结束!请给个评价吧!")
复制代码
1.while temp.isdigit():以及temp.isdigit(): 是什么意思,是当变量temp是数字时进入循环吗
不知道哪里出错了,我看和答案一样呀
- temp = input("请输入一个闰年:")
- while not temp.isdigit():
- temp = input("抱歉,您的输入有误,请重新输入:")
- year = int(temp)
- if year/400 == int(year/400):
- print(year + "是闰年!")
- else:
- if (year/4 == int(year/4)) and ((year/100) != int(year/100)):
- print(year + "是闰年!")
- else:
- print(year + "不是闰年!")
-
复制代码
代码我改进了一下,你可以看看
- answers = random.randint(1, 10)
- times = 1
- guess = 0
- while guess != answers:
- temp = input("从1-10选一个数:")
- if temp.isdigit(): # isdigit 你把最后一个 i 打成了 h
- guess = int(temp)
- if (times <= 3) and (guess == answers):
- print("聪明,这都给你猜中啦")
- elif guess > answers:
- print("大了一丢丢呀,再来一次!")
- else:
- print("小了一点点呢,再试试!")
- times = times + 1 # 改进 1
- else:
- print("不听话,要输入数字:")
- if times > 3: # 这里有跳出,while可以不写条件
- print("三次机会到啦,下次再玩吧")
- break
- print("游戏结束!请给个评价吧!")
复制代码
|
|