python之循环1

循环:while,for

for 循环:

 for i in range(0,10,2): 

     循环体

备注:range里面的2,是步长,就是 i 取值为:0 ,2,4,6,8;默认为1,for i   in range(10)==for i  in range (0,10)==for i  in range (0,10,1)

结束循环:break:结束当前循环

continue:跳出本次循环,进入下一次循环

写个循环:猜一个数字的大小

思路:给出一个数字猜大小,当猜的数字大于给出的数字,返回猜的数字过大;当猜的数字小于给出的数字,则返回猜的数字过小,当猜的数字等于给出的数字,则返回成功

猜的次数限制为3次,当第三次还猜不对的时,询问是否继续猜字游戏,当输入非‘n’和‘N’时,可以继续猜三次。

good_number=57
guess_count=0
while guess_count<3:
guess_number=int(input('Please input your guess_number: '))
if guess_number==good_number:
print('Good!!!You got it !!!')
break
elif guess_number<good_number:
print('Too bad!!!You guessed it was small.')
else:
print('Too bad!!!You guessed it was big.')

guess_count+=1

if guess_count==3:
print('You have guessed wrong three times,do you want to keep guessing...')
continue_game=input('If you want to continue playing,you can enter any character or enter directly;\n'
"If you do not want to continue playing, please enter 'n '.\n")
if continue_game!='n'and continue_game!='N':
guess_count=0

猜你喜欢

转载自www.cnblogs.com/sum123/p/9125378.html