for循环,列表和格式化输出

时间:2023-03-09 07:34:22
for循环,列表和格式化输出

一:for 循环

1.          简单的说如果让你输出1到100之间的整数,用while该怎么实现呢?

i=

while i<:

  print(i)

  i+=

  

看着是不是只有4行,但是有没有更加简单的办法,不妨我们使用for循环来试一下

for i in range(1,101,):
  print(i)
         是不是更加简单,所以说我们总会选择最简单的方法去达到我们的目的
九九乘法表:使用while
while i <= 9:
j = 1
while j <= i:
print(str(j) + "*" + str(i) + "=" + str(j * i), end="\t")
j += 1
print()
i += 1

结果:

1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

如果使用for循环

for循环,列表和格式化输出

2. 输出1到100之间的偶数
for i in range(1,101,):
if i%2==0:
  print(i)

输出

for i in range(1,101,2): print("loop:",i)

3.   break的双重用法
exit_flag=False
for i in range():
if i<:
continue
print(i)
for j in range():
print(j)
if j==:
exit_flag=True
break
if exit_flag:
break
4...
for循环中,如果执行break,则下面的就不执行
(1)            程序: 三次登录
# _user="sang"
# _password=""
# for i in range(,):
# username=input("Username:")
# password=input("Password")
# if username==_user and password==_password:
# print("Welcome %s to login"% _user)
# break#跳出,中断
# else:
# print("Invilid username or password")

   比较:
#for循环更加简单的比较
# _user="sang"
# _password="123"
# for i in range(0,3):
# username=input("Username:")
# password=input("Password")
# if username==_user and password==_password:
# print("Welcome %s to login"% _user)
# break#跳出,中断
# else:
# print("Invilid username or password")
# else:
# print("执行次数太多!")_user="sang"

  

   再次比较:
_user="sang"
_password="123"
passed_anthentication=False #anthentication认证
for i in range(0,3):
username=input("Username:")
password=input("Password")
if username==_user and password==_password:
print("Welcome %s to login"% _user)
passed_anthentication=True
break#跳出,中断
else:
print("Invilid username or password")
if not passed_anthentication:
print("输入次数太多!")

  

二:格式化输出                                基本的格式化,语法为: 格式化符号 % 格式化对象
ctrl d 直接粘贴到下一行 ctrl ?    注释多行
name = input("Name:")
age = input("Age:")
job = input("Job:")
salary = input("Salary:")
msg = '''
------------- info of %s -----------------
Name: %s
Age : %s
Job : %s
Salary: %s
------------ end ----------------
''' % (name,name,age,job,salary)
print(msg)
     加深比较:
name = input("Name:")
age = int(input("Age:"))
job = input("Job:")
salary = input("Salary:")
if salary.isdigit():
salary=int(salary)
else:
print("must input digit")
exit()#退出程序
msg ='''
-------------info of %s -----------------
name: %s
age: %d
job: %s
salary: %f
you will be retired in %s years
------------end----------------
'''% (name,name,age,job,salary,100-age)
print(msg)

三:              列表   list  
(1) a=("aaa","bbb","ccc","ddd","eee") 元组里进行的 切片:取多个元素
print(a[1:])   取到最后面
a[1:4]取下标一到下标4的数字,包括1但是不包括4
print(a[1:-1])取到倒数第二位
print(a[1:-1:1])从左到右一个一个取
print(a[1::2])从左到右隔一个去取
(2)以下这些操作只能在列表里进行: 列表的一些操作命令
#a=["aaa","bbb","ccc","ddd","eee"]
#删除
#a.remove("ccc") remove("内容")
#a.pop(2) pop(index)默认尾部删除,可指定索引值
#del a[2] del a del a[index]
#a.clear()清空
#增加
#a.append("zzz")追加一个数
#a.insert(1,"ccc") a.insert(index,"内容")
#a.extend("h") 扩展
#修改
#a[2]="mmm" a[index]="新的值"
#a[1:3]=[111,222,333] a[start:end]=[a,b,c]
a=[2,5,3,9,2,7]
#a.sort() 排序,默认升序
#a.reverse() 反序
#b=a.count("2") 查找元素的个数
# b=["f","g","h"]
# b.extend(a) 连接列表
print(b) (3)以下是一个简单的购物车程序,
goods_list=[("iphone",),("book",),("watch",),("coffee",),("icecream",)]#定义商品列表
salary=input("Your salary is:") #定义你的工资
shop_list=[] #定义购物车列表
if salary.isdigit():
salary=int(salary)
while True: for i,j in enumerate(goods_list,):
print (i,">>>>>>>>>>>",j)
choice=input("选择需要购买商品的编号[退出: q]:")
if choice.isdigit():
choice=int(choice)
if choice> and choice<=len(goods_list):
p_item=goods_list[choice-]
if p_item[]<salary:
salary-=p_item[]
shop_list.append(p_item)
else:
print("余额不足,还剩%s"%salary)
print(p_item)
else :
print("编码不存在")
elif choice=="q":
print("--------已购买如下-----------商品")
for i in shop_list:
print(i)
print("您还有余额为:%s"%salary)
break
else:
print("invalid input")

 

相关文章