search = '168'
num_a = '1386-168-0006'
num_b = '1681-222-0006'
print(search + ' is at ' + str(num_a.find(search)) + ' to ' + str(num_a.find(search) + len(search)) + ' of num_a')
print(search + ' is at ' + str(num_b.find(search)) + ' to ' + str(num_b.find(search) + len(search)) + ' of num_a')
print(str(num_a.find(search)))
print(len(search))
#运行结果如下:
168 is at 5 to 8 of num_a
168 is at 0 to 3 of num_a
5
3
def g_to_kg(g):
return str(g/1000) + 'kg'
print(g_to_kg(2000))
def pythagorean_theorem(a, b):
return 'The right triangle third side\'s length is {}'.format(((a**2) + (b**2))**(1/2))
print(pythagorean_theorem(3, 4))
# 运行结果如下:
2.0kg
The right triangle third side's length is 5.0
def trapezoid_area(base_up, base_down, height):
return 1/2 * (base_up + base_down) * height
trapezoid_area(1, 2, 3) # 位置参数
trapezoid_area(base_up=1, base_down=2, height=3) # 关键字参数
def text_create(name, msg):
desktop_path = 'C:/Users/ZHENG/Desktop/'
full_path = desktop_path + name + '.txt'
file = open(full_path, 'w')
file.write(msg)
file.close()
print('Done')
text_create('hello', 'hello world')
运行的结果是在桌面建立了一个名为hello的txt文件,内容为hello world。
def text_filter(word, censored_word='lame', changed_word='Awesome'):
return word.replace(censored_word, changed_word)
print(text_filter('Python is lame!'))
#运行结果:
Python is Awesome!
敏感词过滤器
def text_create(name, msg):
desktop_path = 'C:/Users/ZHENG/Desktop/'
full_path = desktop_path + name + '.txt'
file = open(full_path, 'w')
file.write(msg)
file.close()
print('Done')
def text_filter(word, censored_word='lame', changed_word='Awesome'):
return word.replace(censored_word, changed_word)
def censored_text_create(name, msg):
clean_msg = text_filter(msg)
text_create(name, clean_msg)
print(censored_text_create('Try', 'lame!lame!lame!'))
#运行的结果是在桌面建立了一个名为Try的文件,内容为Awesome!Awesome!Awesome!
# 简易密码登陆
def account_login():
tries = 3
while tries > 0:
password = input('Password:')
password_correct = password == password_list[-1]
password_reset = password == password_list[0]
if password_correct:
print('Login success!')
break
elif password_reset:
new_password = input('Enter a new password:')
password_list.append(new_password)
print('Your password has changed successfully!')
account_login()
break
else:
print('Wrong password or invalid input!')
tries = tries -1
print(tries, 'has left!')
else:
print('your times has been run out ,and your account has been suspended!')
account_login()
上述代码实现的主要功能是原始密码为12345,运行程序后则需要输入登陆密码,若输入##则为修改登陆密码。正确则登陆成功,错误则继续输入。 三次都不成功就冻结账户。
for num in range(1,11):
print(str(num) + ' + 1 =', num +1)
# 运行结果:
1 + 1 = 2
2 + 1 = 3
3 + 1 = 4
4 + 1 = 5
5 + 1 = 6
6 + 1 = 7
7 + 1 = 8
8 + 1 = 9
9 + 1 = 10
10 + 1 = 11
songlist = ['Holy Diver', 'Thunderstruck', 'Rebel Rebel']
for song in songlist:
if song == 'Holy Diver':
print(song, ' - Dio')
if song == 'Thunderstruck':
print(song, ' - AC/DC')
elif song == 'Rebel Rebel':
print(song, '-David Bowie')
# 运行结果如下:
Holy Diver - Dio
Thunderstruck - AC/DC
Rebel Rebel -David Bowie
# 输出乘法口诀表
for i in range(1,10):
for j in range(1,10):
print('{} X {} = {}'.format(i,j,i*j))
def text_creation():
path = 'C:/Users/ZHENG/Desktop/'
for name in range(1,11):
with open(path + str(name) + '.txt', 'w') as text:
text.write(str(name))
text.close()
print('Done')
text_creation()
该程序的作用就是在桌面创建10个txt文件,命名为1到10。
#复利计算
def invest(amount, rate, time):
print("principal amount:{}".format(amount))
for t in range(1, time + 1):
amount = amount * (1 + rate)
print("year {}: ${}".format(t, amount))
invest(100, .05, 8)
invest(2000, .025, 5)
#运行结果如下:
principal amount:100
year 1: $105.0
year 2: $110.25
year 3: $115.7625
year 4: $121.55062500000001
year 5: $127.62815625000002
year 6: $134.00956406250003
year 7: $140.71004226562505
year 8: $147.74554437890632
principal amount:2000
year 1: $2050.0
year 2: $2101.25
year 3: $2153.78125
year 4: $2207.62578125
year 5: $2262.8164257812496
# 打印100以内的偶数
def even_print():
for i in range(1, 101):
if i % 2 == 0:
print(i)
even_print()
猜大小游戏
import random
def roll_dice(numbers=3, points=None):
print('start to roll the dice!')
if points is None: # 参数中未指定points则为其创建空的列表
points = []
while numbers > 0: # 摇三次骰子
point = random.randrange(1,7)
points.append(point)
numbers = numbers - 1
return points # 返回结果的列表
def roll_result(total):
isBig = 11 <= total <= 18 # 设定大小的判断标准
isSmall = 3 <= total <= 10
if isBig:
return 'Big'
elif isSmall:
return 'Small'
def start_game():
print('GAME START!')
choices = ['Big', 'Small']
your_choice = input('Big or Small:')
if your_choice in choices: # 规定正确的输入
points = roll_dice()
total = sum(points)
youWin = your_choice == roll_result(total) #胜利的条件
if youWin:
print('The points are', points, 'You win!')
else:
print('The points are', points, 'You lose!')
else:
print('Invalid Words')
start_game()
start_game()
检验手机号
def number_test():
while True:
number = input('Enter Your Number:')
CN_mobile = [134, 135, 136, 137, 138, 139, 150, 151, 152, 157, 158, 159, 182, 183,\
184, 187, 188, 147, 178, 1705]
CN_union = [130, 131, 132, 155, 156, 185, 186, 145, 176, 1709]
CN_telecom = [133, 153, 180, 181, 189, 177, 1700]
first_three = int(number[0:3])
first_four = int(number[0:4])
if len(number) == 11:
if first_three in CN_mobile or first_four in CN_mobile:
print('Operator : China Mobile')
print('We\'re sending verification code via text to your phone:', number)
break
elif first_three in CN_union or first_four in CN_union:
print('Operator : China Union')
print('We\'re sending verification code via text to your phone:', number)
break
elif first_three in CN_telecom or first_four in CN_telecom:
print('Operator : China Telecom')
print('We\'re sending verification code via text to your phone:', number)
break
else:
print('No such operator')
else:
print('Invalid length, your number should be in 11 digits')
number_test()