温馨提示:本文最后更新于
2024-12-11 09:52:52
,某些文章具有时效性,若有错误或已失效,请在下方留言。for 循环
for 循环的基本语法,如下所示
for item in list_of_items:
# do something to each item
示例代码
fruits = ["Apple", "Peach", "Pear"]
for fruit in fruits:
print(fruit)
# Apple
# Peach
# Pear
range 函数
range 函数的语法如下所示
# 包含a,不包含b
for number in range(a, b):
print(number)
示例代码
total = 0
# 包含1,不包含101
for number in range(1,101):
total += number
print(total) # 5050
密码生成器
Random 模块
中的shuffle()
函数可以实现列表元素的随机化
import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
print("Welcome to the PyPassword Generator!")
nr_letters = int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))
password_list = []
for letter in range(0, nr_letters):
password_list.append(random.choice(letters))
for symbol in range(0, nr_symbols):
password_list.append(random.choice(symbols))
for number in range(0, nr_numbers):
password_list.append(random.choice(numbers))
# 列表的随机化
random.shuffle(password_list)
password = ""
for letter in password_list:
password += letter
print(f"Your password is: {password}") # Your password is: o(6+Fm)9M
© 版权声明
THE END
暂无评论内容