In [2]:
#练习一:自己定义一个reverse(s)函数,功能返回字符串s的倒序字符串。
def reverse(s):
print(s[len(s)-1::-1])
s = str(input('enter you string: '))
reverse(s)
In [7]:
#练习二:写函数,根据给定符号和行数,打印相应直角三角形,等腰三角形及其他形式的三角形。
def intro():
print('''
------------------------
| 1 -----> 直角三角形 |
| 2 -----> 等腰三角形 |
| 随便 -----> 没有三角形 |
------------------------
''')
def tri1():
sym = (input('enter your symbol: '))
h = int(input('enter your hight: '))
for i in range(0, h+1):
for j in range(0, i):
print(sym, end = '')
print()
def tri2():
sym = (input('enter your symbol: '))
h = int(input('enter your hight: '))
for i in range(0, h+1):
print(' '*(h-i-1) + sym*(2*i-1))
print()
while True:
intro()
type = int(input('enter the triangle type: '))
if type == 1:
tri1()
elif type == 2:
tri2()
else:
break
In [11]:
#练习三:将任务4中的英语名词单数变复数的函数,尽可能的考虑多种情况,重新进行实现。
def change(word):
if word.endswith('s') | word.endswith('x') | word.endswith('sh') | word.endswith('ch'):
print('词尾加es')
elif word.endwith('f') | word.endswith('fe'):
print('将f变为v,加s')
elif word.endwith('y'):
print('去掉y加ies')
else:
print('直接加s')
word = input('请输入英文单词')
change(word)
In [23]:
#练习四:写函数,根据给定符号,上底、下底、高,打印各种梯形。
sym = (input('enter your symbol: '))
h = int(input('enter your hight: '))
s1 = int(input('enter your shangdi: '))
s2 = int(input('enter your xiadi: '))
for i in range(0, h):
print(sym * (s1+i))
In [37]:
#练习五:写函数,根据给定符号,打印各种菱形。
n = int(input('enter the size(only odd numbers are allowed): '))
for i in range(0,n//2+1):
print((n//2-i)*' ' + ((i*2)+1)*'*')
for i in range(0,n-n//2-1):
print((i+1)*' ' + (n-2*i-2)*sym)
In [28]:
#练习六:与本小节任务基本相同,但要求打印回文字符倒三角形。
def oppo(line):
for i in range(len(line)*2,0,-1):
if i == 1:
print(' '*(len(line)*2-1) + line[0])
elif i%2 == 1:
print(' '*(len(line)*2-i) + line[:i//2] + line[i//2] + line[i//2-1::-1])
else:
print(' '*(len(line)*2-i) + line[:i//2] + line[i//2-1::-1])
def main():
string = '赏花归去马如飞'
oppo(string)
if __name__ == '__main__':
main()