#while循环
'''
while 条件语句
功能
重点:
while的掌握
break (结束循环)
continue (跳过本次循环,直接执行下一个循环)
pass (占位符主要用于写函数的时候测试用)
'''
#条件语句为真就成立
# import random
# num= random.randint(1,999)
# print(num)
# count = 0
#
# while count < 100:
# count +=1
# print(count,"萨格王,格萨王,格格萨王")
# import random
# lucky_num = random.randint(1,100)
# print(lucky_num)
# lucky_num_g = lucky_num % 10
# lucky_num_s = lucky_num // 10
#
# while True:
# buy_num = int(input("请输入你的数字:"))
# buy_num_g = buy_num % 10
# buy_num_s = buy_num // 10
# if lucky_num == buy_num:
# print("恭喜你,你的数字很准确!")
# break
# elif lucky_num_g == buy_num_s and lucky_num_s == buy_num_g:
# print("恭喜你猜的颠倒了!")
# break
# elif lucky_num_g == buy_num_g or lucky_num_g == buy_num_s or lucky_num_s == buy_num_g or lucky_num_s == buy_num_s:
# print("你猜得对了其中一个数字!")
# break
# else:
# print("你的数字部队,大侠请重新来过,萨格王,格萨王,格格萨王!")
# import random
# num = random.randint(1,500)
# print(num)
# print("如果你不想玩了,你输入-1会自动退出")
#
# while True:
# buy_num = int(input("请输入一个数字,我会给予你提示:"))
# if buy_num == -1:
# break
# elif buy_num > 500:
# print("waring:!请输入小于500的数字!")
# continue
# elif buy_num > num:
# print("你猜的太大了!")
# elif buy_num < num:
# print("你猜的太小了")
# else:
# print("恭喜你猜对了!!萨格王 ,格萨王,格格萨王!")
# a = 1
#
# while a < 10:
# b = 1
# while b <=a:
# print(b,"x",a,"=",b*a,end=" ")
# b += 1
# print()
# a +=1
# a = 9
# while a > 1:
# b = 1
# while b <= a:
# print(end=" ",b,"*",a,"=",b*a)
# b +=1
# print()
# a-=1
# a = 1
# while a < 10:
# b = 1
# while b <= a:
# print(b,"*",a,"=",b*a,end=" ")
# b += 1
# print()
# a +=1
# c = 1
# while c<10:
# d=1
# while d<=c:
# print(c,"*",d,"=",c*d,end=" ")
# d +=1
# print()
# c +=1
# a = 9
# while a>1:
# b = 1
# while b<=a:
# print(b,"*",a,"=",b*a,end=" ")
# b +=1
# print()
# a -=1
# a = 1
# while a <10:
# b = 1
# while b <=a:
# print(b,"*",a,"=",b*a,end=" ")
# b +=1
# print()
# a +=1
# i = 1
# while i < 10:
#
# k = 9
# while k > i:
# print(end=" ")
# k -= 1
#
# j = 1
# while j <= i:
# print("%sx%s=%s"%(j,i,j*i),end="\t")
# j += 1
# print()
# i += 1
#for循环
#主要用于获取遍历
#使用for循环拿出字符串中的内容
# var = "今天,我感觉很累,一会早点休息了"
# for val in var:
# print(val)
#使用for循环拿出列表的内容
# list1=["今天","很累","呢"]
# for var in list1:
# print(var)
#使用for循环拿出元组的内容
# var = ("明天","希望","变好")
# for val in var:
# print(val)
#使用for循环拿出集合的内容
# var = {1,2,3,5}
# for val in var:
# print(val)
#使用for循环拿出字典的内容
# dict1 = {"今天":"晚上","我在":"代码","好累":"好爽"}
# for var in dict1: (这个方法是取出字典中的主键)
# print(var)
#下面我们来取出这个字典中的值
# var1 = {"yes":"nod","abc":"123","cde":"456"}
# for val in var1:
# print(val)
# print(var1.values())
# for val in var1.values():
# print(val)
# var = {"a":1,"b":2,"c":3}
# for val in var:
# print(val)
# print(var.values())
# for val in var.values():
# print(val)
# for key,value in var.items():
# print(key,value)
# dict1 = {"a":"1","b":"2","c":"3"}
# for key,value in dict1.items():
# print(key,value)
# dict1 = {"a":"1","b":"2","c":"3"}
# for val in dict1:
# print(val)
# print(dict1.values())
# var = {"a":"1","b":"2","c":"3"}
# for key,value in var.items():
# print(key,value)
#----------------------------------
# dict = {"我":"感觉","好":"累","真的":"好累"}
# for var,val in dict.items():
# print(var,val)
#设置一个字典,要求拿出字典中的值
# dict1 = {"a":"1","b":"2"}
# for var in dict1.values():
# print(var)
#设置一个字典要求拿出字典中的主键和值
# a = {"asd":"asdd","asddd":"asdddd"}
# for key,value in a.items():
# print(key,value)
#设置一个字典要求拿出他的主键然后在拿出他的值
# dict1 = {"a":"1","b":"2","c":"3"}
# for var in dict1:
# print(var)
# print(dict1.values())
# var = {"today":"is","good":"day"}
# for val in var:
# print(val)
# print(var.values())
# var = {"abc":"123","cde":"456"}
# for key,value in var.items():
# print(key,value)
# var = ["c++","java","python"]
# for val in var:
# print(val)
# for res in val:
# print(res)
# var = {"a":"1","b":"2"}
# for val in var:
# print(val)
# print(var.values())
# var = {"name":"yangyang","name2":"yangyang2"}
# for val,res in var.items():
# print(val,res)
# var = [[1,2,3],[4,5,6],[7,8,9]]
# for val in var:
# print(val,type(val))
# for res in val:
# print(res)
# for a,b,c in var:
# print(a,b,c)
#range()形成一个序列
# res = range(1,10)
# for var in res:
# print(var)
# for i in range(1,10):
# for j in range(1,i+1):
# print("%sx%s=%s"%(j,i,j*i),end=" ")
# print()
# for i in range(1,10):
# for j in range(1,i+1):
# print("%s*%s=%s"%(j,i,j*i),end=" ")
# print()
# for i in range(1,10):
# for j in range(i,10):
# print("%s*%s=%s"%(j,i,j*i),end=" ")
# print()
# for i in range(1,10):
# for k in range(1,10-i):
# print(end = " ")
# for j in range(1,i+1):
# print("%sx%s=%s"%(j,i,j*i),end="\t")
# print()
#.items(参数,取出字典中的所有内容,键值都拿出来)
# var = {"a":"b","c":"d"}
# for val in var.items():
# print(val)
#当然如果字典中的值都是一样的,可以直接设置两个值来拿出来
#显示起来更加的直观
#使用两个变量进行接收
# dict1 = {"a":"1","b":"2"}
# for var,val in dict1.items():
# print(var,val)
#range()
#生成一个序列,(容器)
#用法: 变量名 = range() :括号里面写需要的字符,默认从0开始,但可以自定义起始字符
#使用9*9表
#等一一个i,然后定义一个序列,数字从1-9,然后依次拿出里面的内容
# for i in range(1,10):
#然后定义一个j,也定义一个序列,他的序列从1开始,到i+1结束,
#j的循环是1,i+1,那么i每次都是1,2,3这样循环,j的循环结束每次都比i多1
#所以数据从i进来,i的值为1,然后进行内循环,内循环j的值先开始为1,所以往下走,i=1,j=1,1*1=1,之后在进行循环,因为j循环中的值并没有被循环完,i+1嘛,所以j的值是2,内循环还可以继续循环,所以就是1*2=2,之后j的值没了,循环随之结束,换行,之后在进行外部循环,这次i的值在进行循环拿出2,然后依次进行循环
# for j in range(1,i+1):
# print("%s*%s=%s"%(j,i,j*i),end=" ")
# print()
# for i in range(1,10):
# for j in range(i,10):
# print("%s*%s=%s"%(j,i,j*i),end=" ")
# print()
# for i in range(1,10):
# for j in range(1,i+1):
# print("%s*%s=%s"%(j,i,j*i),end=" ")
# print()
# for i in range(1,10):
# for k in (1,10-i):
# print(end=" ")
# for j in range(1,i+1):
# print("%s*%s=%s"%(j,i,j*i),end="\t")
# print()
# count = 0
# sum1 = 0
# while count < 101:
# sum1 += count
# count +=1
# print(sum1)
# height = 100
# sum1 = 0
# while True:
# if height < 1:
# sum1 -= height
# break
# else:
# sum1 += height + height/2
# height /= 2
# print(sum1)
python循环十次结束
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
11个Python循环技巧
本文将介绍如何使用循环在Python中创建多个列表,并提供代码实例。
Python 数据 字符串 列表 循环