python之迴圈1

來源:互聯網
上載者:User

標籤:int   大小   and   進入   bad   python   ssi   ber   too   

迴圈: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

 

python之迴圈1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.