for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
if i!=j and j!=k and i !=k:
print(i,j,k)
else:
pass
分析:1.输入利润 2.求奖金总数
繁琐写法
i=input('请输入当月利润值(万):')
i=float(i)
if i<=10:
print('当月奖金为:',i*10/100)
elif i<=20:
print('当月奖金为:', (10 * 10 / 100)+(i-10)*7.5/100)
elif i <= 40:
print('当月奖金为:', (20 * 10 / 100) + ((i - 20) * 5 / 100) )
elif i<=60:
print('当月奖金为:', (40 * 10 / 100) + ((i - 40) * 3 / 100))
elif i<=100:
print('当月奖金为:', (60 * 10 / 100) + ((i - 60) * 3 / 100))
else:
print('当月奖金为:', (100 * 10 / 100) + ((i - 100) * 1 / 100))
优化后写法
# !/usr/bin/python
# -*- coding: UTF-8 -*-
i=float(input('请输入利润(万):'))
arry=[100, 60, 40, 20, 10,0]
rate=[0.01, 0.015,0.03, 0.05, 0.075, 0.1]
sum=0
for k in range(0,6):
if i>arry[k]:
sum +=(i-arry[k])*rate[k]
print((i-arry[k])*rate[k])
i=arry[k]
print(sum)
k=1
for i in range(1,51):
if k<=7:
print('数字:',i)
while i > 0:
print('*',end='')
i -= 1
k+=1
print('\n')
else:
break
打开文件,连接数据库
import pymysql
db=pymysql.connect(host='47.96.235.136',
port=3307,
user='dbmon',
passwd='123456',
db='sakila'
charset='utf8' #charset很重要,不添加在sql语句插入汉字时报错。
#UnicodeEncodeError: 'latin-1' codec can't encode characters in position 36-41: ordinal not in range(256)
)
cur = db.cursor()
#cur.execute("create table city1(city_id int ,city varchar(50),country_id int ,last_update timestamp)")
with open('city.txt','r') as df:
for line in df.readlines():
if line:
list=[]
list=line.encode('utf-8').decode().strip().replace('\"','').split('\t')
print(type(list))
sql='insert into city1 values (%s,%s,%s,%s)'
cur.execute(sql , list)
db.commit()
cur.close()
db.close()
print('完成')
import pymysql
db=pymysql.connect(host='47.96.235.136',
port=3307,
user='',
passwd='',
db='sakila'
)
cur = db.cursor()
#cur.execute("create table city1(city_id int ,city varchar(50),country_id int ,last_update timestamp)")
with open('city.csv','r') as df:
for line in df.readlines():
if line:
list=[]
list=line.encode('utf-8').decode().strip().replace('\"','').split(sep=',')
print(type(list))
print(list)
sql='insert into city1 values (%s,%s,%s,%s)'
cur.execute(sql , list)
db.commit()
cur.close()
db.close()
print('完成')
import pandas as pd
import numpy as np
df=pd.read_csv('city.csv',header=None)
count=df.count()[0]
j=0
while j < count:
line = df.iloc[j, :]
line_str = []
for i in line:
line_str.append(i)
print(line_str)
j+=1