# 前20行有行号,第21行结束,无行号
k = 1 # 设定循环初始次数
TestString = "Hello Python" # 测试字符串
print("#"*10+"输出结果如下"+"#"*10)
while 1:#开始循环
\tif k <= 20:#次数在20以内
\t\tprint(f"第{k}行,{TestString}") # 带行号
\telse:# 总次数超过20次
\t\tprint(f"最后一行,{TestString}") # 不带行号
\t\tbreak # 退出循环
\tk += 1 # 次数累加
print("#"*12+"输出结束"+"#"*12)\n''')
print(text) # 查看代码
exec(text) # 打印结果
import csv
def bmi(*list):
info = list[0]
height = float(info[0])
weight = float(info[1])
bmi = weight/(height*height)
return str(bmi)
row1=[]
row2=[]
row3=[]
a = open('data.csv', 'r', encoding='utf-8')
reader = csv.DictReader(a)
for r in reader:
row1.append(r['我'])
row2.append(r['妹妹'])
row3.append(r['哥哥'])
a.close()
row = ['bmi', bmi(row1), bmi(row2), bmi(row3)]
out = open("data.csv", "a", newline="")
csv_writer = csv.writer(out, dialect="excel")
csv_writer.writerow(row)
out.close()
class Price:ticket_d = 100
ticket_w = ticket_d*1.2
def rq(self):
self.a = int(input('请输入是平日还是周末(平日:1/周末:0):'))
if self.a == 0:
self.p = self.ticket_w
if self.a == 1:
self.p = self.ticket_d
def pj(self):
input('请输入是人数' + '\n')
self.ad = int(input('大人数量:'))
self.ch = int(input('儿童数量:'))
self.money = self.p *self.ad + self.p *self.ch/2
print('%f' % self.money)
class Ticket():
def __init__(self, weekend=False, child=False):
self.exp = 100
if weekend:
self.inc = 1.2
else:
self.inc = 1
if child:
self.discount = 0.5
else:
self.discount = 1
def calcPrice(self, num):
return self.exp * self.inc * self.discount * num
adult = Ticket()
child = Ticket(child=True)
print("2个成人 + 1个小孩平日票价为:%.2f" % (adult.calcPrice(2) + child.calcPrice(1)))
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)