写在前面
第七弹最后一个综合练习部分出了点小问题,为了不影响进度那我先进行第八弹的梳理,之后有机会再把第七弹补上。
Python Round⑧
倒序
animals = ['cow', 'duck', 'cat', 'dog']
s = animals[::-1]
print(s)
另:答案上采用的是另一种方案:
animals = ['cow', 'duck', 'cat', 'dog']
animals.reverse()
print(animals)
删除空白格
word = " 窗前明月光,疑是地上霜。 "
print(word.strip())
输出1-50间的奇数
count = 0 #其实和for理念差不多,都是从0开始的
while count < 50:
count += 1
if count % 2 == 0:
continue
print(count,end=",")
海龟库
import turtle
for i in range(4):
turtle.seth(90*i+90)
turtle.circle(50,90)
turtle.seth(90*i-90)
turtle.circle(50,90)
turtle.hideturtle()
对数异常处理
import math
try:
a = eval(input('请输入底数:'))
b = eval(input('请输入真数:'))
c = math.log(b,a)
except ValueError:
if (a > 0) and (b <= 0):
print("真数必须大于0")
elif (b > 0) and (a <= 0):
print("底数必须大于0")
elif (b <= 0) and (a <= 0):
print("真数底数必须大于0")
except ZeroDivisionError:
print('底数不能为1')
except NameError:
print('输入必须为实数')
else:
print(c)
字符转换(chr()、ord())
intxt = input("请输入明文:")
for i in intxt:
if "a" <= i <= "z":
print(chr(ord("a")+(ord(i)-ord("z")+3)%26),end=" ")
elif "A" <= i <= "Z":
print(chr(ord("A")+(ord(i)-ord("Z")+3)%26),end=" ")
else:
print(i,end=" ")
ord()函数主要用来返回对应字符的ascii码
chr()主要用来表示ascii码对应的字符