|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
哪里出问题了- import random
- times = 3
- no = random.randint(1,100)
- name = input("请输入您的姓名:")
- print('你好,' + name + '!''开始答题')
- temp = input("1+1:")
- guess = int(temp)
- while guess != 2:
- if guess < 2:
- print('小了')
- if guess > 2:
- print("大了")
- temp = input("新的填入:")
- guess = int(temp)
- print("通知:正确")
- temp = input("有时候运气也是实力的一种,猜1~100的数:")
- yet = int(temp)
- while (yet != no) and (times > 0):
- if yet < no:
- print('小了')
- if times > 0:
- print("再试一次吧:", end=" ")
- temp = input("新的填入:")
- yet = int(temp)
- else:
- print("机会用光咯T_T")
- if yet > no:
- print("大了")
- if times > 0:
- print("再试一次吧:", end=" ")
- temp = input("新的填入:")
- yet = int(temp)
- else:
- print("机会用光咯T_T")
-
- times = times - 1 # 用户每输入一次,可用机会就-1
- print("通知:结束")
复制代码
[b]
你具体遇到什么问题了吗?看了下代码,粗略看出一个小问题
就是最后的 while 循环中, 第一个用 if 第二个应该用 elif
否则当 yet 小于随机数时,且再试一次的输入大于 随机数,就会再次进入 第二个 if yet > no ,导致一次循环将两个 if 都执行了,但是 times 没有减少
这样就会导致输入次数和你设置的次数不匹配,第二个 if 改成 elif 后就不会出现上述问题
[/b]
|
|