python 基础之循环

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 基础之循环相关的知识,希望对你有一定的参考价值。

  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) ##56,,78

 









































































































以上是关于python 基础之循环的主要内容,如果未能解决你的问题,请参考以下文章

Python入门基础之条件判断和循环

常用python日期日志获取内容循环的代码片段

Python基础学习代码之条件和循环

python基础之条件和循环

Python基础知识之二

Python的语言基础总结之循环与字符串操作