basic programming python: Python applet basis of a simple example code

This article introduces the basic Python simple applet code examples, very good, has a certain value for references, a friend in need can refer to
a multiplication table

for i in range(9):#从0循环到8
  i += 1#等价于 i = i+1
  for j in range(i):#从0循环到i
    j += 1
    print(j,'*',i,'=',i*j,end = ' ',sep='')
    # end默认在结尾输出换行,将它改成空格 sep 默认 j,'*',i,'=',i*j 各元素输出中间会有空格
  print()#这里作用是输出换行符
i = 1
while i <= 9:
  j = 1
  while j <= i:
    print("%d*%d=%d" % (j,i,i*j),end=' ') # 格式化输出
    j += 1
  i += 1
  print()

2 login program

Login procedure, three times with username, password chances mistake three times Account lockout

user = 'hello world'
paswd = 123456
username = input("请输入用户名:")
password = input("请输入密码:")
for i in range(3):
  if username == user and int(password) == paswd: #判断用户名和密码是否都匹配
    print("欢迎您的到来")
    break
  elif i < 2:
    username = input("请输入用户名:")
    password = input("请输入密码")
  elif i == 2:
    print("账户已锁定")
    break

3, shopping cart program

Features:
1, the balance of input
2, show product list
3, enter what you want to buy
4 to determine whether there is the ability to purchase
5 whether to continue
6, the output has been purchased and balances

shop_car = []#用来存放购买的商品
goods = {
  1:['手机',2500],
  2:['电脑',3500],
  3:['自行车',4500],
  4:['宝马',20000]
}#商品列表
while True:
  salary = input("你有多少钱:") # 输入有多少钱
  if salary.isdigit(): # 判断是否为整数
    salary = int(salary) # 将字符串转化为整数
    print("是否要买东西:")
    flag1 = input("Y   N:")
    if flag1.upper() == 'N': # 将字符串大写
      exit("欢迎下次光临") # 退出程序并输出“欢迎下次光临”
    elif flag1.upper() == 'Y':
      break        # 终止循环
    elif flag1.upper() == 'Q':
      exit("欢迎下次光临")
  elif salary.upper() == 'Q':
    exit("欢迎下次光临")
while True:
    print("淘宝".center(30,'-')) # 输出以-----淘宝------
    for i in goods: # 循环输出
      print(i,goods[i])
    print("淘宝".center(30, '-'))
    choice_good = input("请输入商品编码:")#接受一个字符串
    if choice_good.isdigit() :
      choice_good = int(choice_good)
      if choice_good >= 1 and choice_good <= 4:
        if salary >= goods[choice_good][1]:
          shop_car.append(goods[choice_good][0]) # 给字典中添加元素
          salary = salary - goods[choice_good][1]
          print("您购买的商品为:", goods[choice_good][0])
          print("余额为:", salary)
          print("是否继续:")
          contin = input("Y  N")
          if contin.upper() == 'N':
            break
          elif contin.upper() == 'Q':
            break
        else:
          print('余额不足')
          print("是否继续:")
          contin = input("Y  N")
          if contin.upper() == 'N':
            break
          elif contin.upper() == 'Q':
            break
      else :
        print("没有这个编号")
        continue # 暂停本次循环
    elif choice_good.upper() == "Q" :
      break
    else:
      print("我看不懂。。。")
print("你买了:",end =' ')
for i in shop_car:
  print(i,end=' ')
print()
print("余额为:",salary)
print("欢迎下次光临")

Finally, I recommend a good reputation python gathering [ click to enter ], there are a lot of old-timers learning skills, learning experience, interview skills, workplace experience and other share, the more we carefully prepared the zero-based introductory information on actual project data method, every day Python programmers explain the timing of technology, learning to share some small details need to pay attention and
summary

The above is a simple Python example code base of the applet small series to introduce, we want to help,

Published 15 original articles · won praise 2 · views 10000 +

Guess you like

Origin blog.csdn.net/haoxun11/article/details/104908003