while循環語句

while循環語句

循環打印1-100,第50次不打印,第60-80次,打印對應值的平方

count = 0
while count < 100:
count += 1
if count == 50: #當count爲50時,跳出此次循環
continue
elif 60 <= count <= 80:
print(count**2)
else:
print(count)

·優化猜年齡遊戲,允許用戶最多猜3次,中間猜對了,直接跳出循環,猜了3次後,再問是否還想玩,如果用戶選y,剛再允許猜3次,以次往復.

age = 25
count = 0
while count < 3: #當次數小於 3次是允許用戶遊戲
count +=1
guess_age = int(input('請猜我的年齡是:'))
if guess_age < 25:
print('年齡太小了,再猜大點')
elif guess_age == 25:
print('恭喜你猜對了')
break
else:
print('年齡太大了,再猜小點')
while count ==3: #用戶3次都猜錯後,詢問是否繼續遊戲
is_play = input('你還想玩嗎?y or n')
if is_play == 'y' or is_play == 'Y':
count = 0 #當用戶選擇繼續遊戲時,將count=0
else:
print('退出遊戲')
break

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章