猜數字遊戲:#!/usr/bin/env python
# -*- coding:utf-8 -*-
age_of_xcn = 20
for i in range(3):
guess_age = int(input("guess age:"))
if guess_age == age_of_xcn:
print("yes,you got it")
break
elif guess_age > age_of_xcn:
print("think smaller")
else:
print("think bigger")
else:
print("bay bay")
執行結果:
guess age:1
think bigger
guess age:2
think bigger
guess age:4
think bigger
bay bay
程序已結束,退出**
#只列印偶數(10以內)#!/usr/bin/env python
# -*- coding:utf-8 -*-
for i in range(0, 10, 2):
print("loop", i)
執行結果:
loop 0
loop 2
loop 4
loop 6
loop 8
猜數字遊戲:
當使用者輸入3後則會提示「do you want to keep ?」輸入知道不是n,則count=0,然後重新從0開始迴圈#!/usr/bin/env python
# -*- coding:utf-8 -*-
age_of_xcn = 20
count = 0
while count
guess_age = int(input("guess age:"))
if guess_age == age_of_xcn:
print("yes,you got it")
break
elif guess_age > age_of_xcn:
print("think smaller")
else:
print("think bigger")
count += 1
if count == 3:
confirm = input("do you want to keep ?")
if confirm != 'n':
count = 0
執行結果:
guess age:1
think bigger
guess age:2
think bigger
guess age:3
think bigger
do you want to keep ?ere
guess age:
Python for 迴圈語句
python for迴圈可以遍歷任何序列的專案,如乙個列表或者乙個字串。語法 for迴圈的語法格式如下 for iterating var insequence statements s 流程圖 例項 usr bin python coding utf 8 forletterin python 第乙...
Python for語句應用
str hello 字串hello for n in str print n,end for numin range 1,101 1 100 print num n int input 請輸入行數 for trigon in range n range 0,n for tri in range tr...
PYTHON for迴圈練習
1.使用者登陸程式需求 1.輸入使用者名稱和密碼 2.判斷使用者名稱和密碼是否正確?name root passwd westos 3.為了防止暴力破解,登陸僅有三次機會,如果超過三次機會,報錯提示 for i in range 3 name raw input 使用者名稱 passwd raw i...