python 基礎之迴圈

來源:互聯網
上載者:User

標籤:特殊   png   strip   數字   nbsp   格式   字母   blog   2.x   

  1.1 變數命令

    nums_of_gf = 19

    NumsOfAlexGf = 19

    不能  .name-of-alex-gf =19

    中間不能有加減號,空格

    數字,特殊字元 不開頭

    關鍵字不能定義變數

    只能以字母,底線開頭

 

  1.2  input

    python3.x input()

    python2.x raw_input()

    python2.x input() >輸入是什麼格式就輸出什麼格式

    name = input("please input your name")

    print ("your name is %s" % name)

    如果輸入是數字類型並且還要對數字操作,最好用ini強制轉換

    raw_input 輸出的是字串格式,需要轉換成數字類型

    input_num = int(raw_inout ("please input your num:")) 

 

 


name = raw_input("name:").strip() ## 去掉前後的空格,不能去掉中間的空格,但是也可以指定去什麼,只能去前後的
age = raw_input("age:")
job = raw_input("job:")
print ("Info1 of []:" + name +"\nName:[]" + name +"\nAge:[]" + age +"\nJob:[]" + job)
print ("Info2 of %s:\nName:%s\nAge:%s\nJob:%s" %(name,name,age,job))
##段落
msg = ‘‘‘
Info3 of %s:
Name:%s
Age:%s
job:%s
‘‘‘ %(name,name,age,job)
print (msg)

 

 

 

 

 

  1.3  if ...else

 

 

 

迴圈 3次 猜數字

 while 多次嵌套,可以用tag 作為變數 判斷

tag = True

while tag:

  ......

  ......

  tag =False

  countinue

 

 

luck_num = 6
time = 0
num = None
while luck_num != num :
num = int(raw_input("please input your num:"))
if luck_num > num:
print ("your num is less than ")
elif luck_num < num:
print ("your num is more than")
time += 1
if time > 3:
print ("your guess time more than 3 time")
break
else:
print ("ok")

 

luck_num = 6
time = 0
num = None
while luck_num != num and time <3:
num = int(raw_input("please input your num:"))
if luck_num > num:
print ("your num is less than ")
elif luck_num < num:
print ("your num is more than")
time += 1
# if time > 3:
# print ("your guess time more than 3 time")
# break
else:
if luck_num == num:
print ("ok")
else:
print ("too much retrys")

   

 

luck_num = 6
time = 0
num = None
while luck_num != num and time <3:
num = int(raw_input("please input your num:"))
if luck_num > num:
print ("your num is less than ")
elif luck_num < num:
print ("your num is more than")
time += 1
# if time > 3:
# print ("your guess time more than 3 time")
# break

if luck_num == num:   ## 可以在while 外面重新判斷
 print ("ok")
else:
  print ("too much retrys")

 


luck_num = 6
time = 0
num = None
while time <3:
num = int(raw_input("please input your num:"))
if luck_num > num:
print ("your num is less than ")
elif luck_num < num:
print ("your num is more than")
else:
print ("ok")
break
time += 1
# if time > 3:
# print ("your guess time more than 3 time")
# break
else:
print ("too much retrys")

 

 

 

 

 

 

for i in range(10):
print i

0
1
2
3
4
5
6
7
8
9

 

## 多層循環
for j in range(5):
for i in range(10):
if i<5:
continue # i <5 就跳出本次循環
# print (i) ## 5 ,6 ,7 ,8 ,9
# if i > 8:
# break
# print (i) ## 5 ,6 ,7 8
if j>3:
break #跳出本層的循環
print (i) ##5,6,,7,8

 

python 基礎之迴圈

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.