Python控制流

python學習代碼都來之於 《簡明Python教程》最新版本 基於Python3版本

if:

number = 23
guess = int(input('Enter an integer:'))
if guess == number:
	#新的代碼塊從裏開始
	print('Cougratulations,youguess it.')
	print('(but you do not win any prizes!)')
elif guess < number:
	print('NO,it is a little higher than that')
else:
	print('NO,it is a little lower than that')
print('Done')
number = 23
guess = int(input('Enter an integer : '))
if guess == number:
   # 新塊從這裏開始
    print('Congratulations, you guessed it.')
    print('(but you do not win any prizes!)')
   # 新塊在這裏結束
elif guess < number:
   # 另一代碼塊
    print('No, it is a little higher than that')
   # 你可以在此做任何你希望在該代碼塊內進行的事情
else:
    print('No, it is a little lower than that')
   # 你必須通過猜測一個大於(>)設置數的數字來到達這裏。
print('Done')
# 這最後一句語句將在
# if 語句執行完畢後執行。
for:
for i in range (1,5):
	print(i)
else:
	print('this for loop is over:')
while:

number = 23
running = True
while running:
	guess = int(input('Enter an integer : '))
	if guess == number:
		print('Congratulations, you guessed it.')
		# 這將導致 while 循環中止
		running = False
	elif guess < number:
		print('No, it is a little higher than that.')
	else:
		print('No, it is a little lower than that.')
else:
	print('The while loop is over.')
# 在這裏你可以做你想做的任何事
print('Done')

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