第一题
定义个函数,保存在另一个py文件里,比如text.py,使用的时候可以直接 import,然后使用。
def fun(a,b):
if a==b:
print('两数一样大')
else:
print(f'{max(a,b)}比较大')
使用的时候:
import test
test.fun(1,2)
第二题:
s = input()
with open('test.txt','w') as f:
f.write(s.upper())
第三题:
class ADD():
def __init__(self,v):
self.v = v
def __add__(self, other):
return [x+y for x,y in zip(self.v,other.v)]
a = ADD([11,32,3,6])
b = ADD([12,16,12,13])
print(a+b)
第四题:
class Animal():
def __init__(self,name):
self.name = name
def eat(self):
print(f'{self.name} is eating')
def drink(self):
print(f'{self.name} is drinking')
def run(self):
print(f'{self.name} is running')
def sleep(self):
print(f'{self.name} is sleeping')
class Dog(Animal):
def bark(self):
print(f'{self.name} is barking')
class Xiaotian(Dog):
def fly(self):
print(f'{self.name} is flying')
a = Xiaotian('xiaotian')
a.eat()
a.drink()
a.run()
a.sleep()
a.bark()
a.fly()