parent
1ef5bf058a
commit
10510481e4
|
@ -81,21 +81,22 @@ Author: 骆昊
|
|||
"""
|
||||
import random
|
||||
|
||||
answer = random.randint(1, 100)
|
||||
counter = 0
|
||||
answer = random.randint(1, 100) #随机值赋值答案
|
||||
counter = 0 #计数器
|
||||
while True:
|
||||
counter += 1
|
||||
counter += 1 #每回答一次计数器+1
|
||||
number = int(input('请输入: '))
|
||||
if number < answer:
|
||||
print('大一点')
|
||||
elif number > answer:
|
||||
print('小一点')
|
||||
else:
|
||||
print('恭喜你猜对了!')
|
||||
break
|
||||
print('你总共猜了%d次' % counter)
|
||||
if counter > 7:
|
||||
print('你的智商余额明显不足')
|
||||
print('恭喜你答对了')
|
||||
print('你共猜了%d次' % counter)
|
||||
if counter >= 3:
|
||||
print('——' * 20 + '\n你共猜了%d次' % counter)
|
||||
print('你的智商明显不足')
|
||||
break #循环满足此条件后打破循环
|
||||
```
|
||||
|
||||
上面的代码中使用了`break`关键字来提前终止循环,需要注意的是`break`只能终止它所在的那个循环,这一点在使用嵌套的循环结构(下面会讲到)需要引起注意。除了`break`之外,还有另一个关键字是`continue`,它可以用来放弃本次循环后续的代码直接让循环进入下一轮。
|
||||
|
|
Loading…
Reference in New Issue