In [11]:
##自己定义一个reverse(s)函数,功能返回字符串s的倒序字符串。
def reverse (s):
print(s[len(s)-1::-1])
s=str(input("plz enter a string"))
reverse(s)
In [12]:
##写函数,根据给定符号和行数,打印相应直角三角形,等腰三角形及其他形式的三角形。
def triangle (x):
for i in range (x):
if i<x-1:
print('|'+' '*i+'\\')
elif i==x-1: print('|'+'_'*i+'\\')
print(' ')
for i in range (x):
if i<x-1:
print(' '*(x-i)+'/'+' '*(2*i)+'\\')
elif i==x-1:
print(' '+'/'+'_'*(i+x-1)+'\\')
x=int(input("plz enter the height of the triangle\n"))
triangle(x)
In [3]:
##将任务4中的英语名词单数变复数的函数,尽可能的考虑多种情况,重新进行实现。
def change(word):
print('The plurality of the noun is ',end='')
if word.endswith('ch') or word.endswith('sh'):
print(word+'es')
elif word.endswith('x') or word.endswith('s') or word.endswith('es'):
print(word+'es')
elif word.endswith('f'):
print(word[:len(word)-1:]+'ves')
elif word.endswith('y'):
print(word[:len(word)-1:]+'ies')
else: print(word+'s')
word=str(input("plz enter a countable noun\n"))
change(word)
In [10]:
##写函数,根据给定符号,上底、下底、高,打印各种梯形。
def tra (x,y,h):
d=h-1
for i in range(h):
if i==0:
print(' '*(d-i)+'/'+'-'*x+'\\'+' '*(d-i))
elif i<h-1:
print(' '*(d-i)+'/'+' '*(x+2*(i))+'\\'+' '*(d-i))
elif i==h-1:
print(' '*(d-i)+'/'+'_'*(2*i+x)+'\\')
x=int(input('请输入梯形的上底长'))
h=int(input('请输入梯形的高'))
tra(x,y,h)
In [9]:
##写函数,根据给定符号,打印菱形。
def rhombus (x):
for i in range (x):
print(' '*(x-i)+'/'+' '*(2*i)+'\\')
for i in range(x-1,-1,-1):
print(' '*(x-i)+'\\'+' '*(2*(i))+'/')
x=int(input('请输入菱形的边长 '))
rhombus(x)
In [14]:
##打印回文字符倒三角形
def inverted(x):
for i in range(len(x)*2,0,-1):
if i == 1:
print(' '*(len(x)*2-1) +x[0])
elif i%2 == 1:
print(' '*(len(x)*2-i) + x[:i//2] + x[i//2] + x[i//2-1::-1])
else:
print(' '*(len(x)*2-i) + x[:i//2] + x[i//2-1::-1])
def main():
word = '北京语言大学'
inverted (word)
if __name__ == '__main__':
main()