1. 猜数字2. 货币转换器3. 骰子模拟器4. 密码记录器5. 赫夫曼编码
1. 猜数字描述
生成一个随机数,然后使用循环给用户三次猜测机会,根据用户的猜测打印最终的结果。
python代码
import random
number = random.randint(1, 10)
for i in range(0, 3):
guess = int(input("guess the number in 1~10: "))
if guess == number:
print("Winning")
print(f"You guess the number right. It's {number}")
break
elif guess > number:
print("You guess is higher")
elif guess < number:
print("You guess is lower")
else:
print(f"Nice Try! But the number is {number}")
输出
2. 货币转换器描述
编写一个Python脚本,可以将一种货币转换为其他用户选择的货币。 使用Python中的API,或者通过forex-python模块来获取实时的货币汇率。 安装:forex-python
python代码
- 获取汇率
from forex_python.converter import CurrencyRates
c = CurrencyRates()
print(c.get_rates('USD') )
- 汇率转换
result = c.convert('USD', 'INR', 10)
- 货币名称
{‘GBP’: 0.7253107441, ‘HKD’: 7.7767750724, ‘IDR’: 14531.0999489188, ‘ILS’: 3.3330495488, ‘DKK’: 6.3322833305, ‘INR’: 73.4100970543, ‘CHF’: 0.9449174187, ‘MXN’: 20.3296441342, ‘CZK’: 22.2075600204, ‘SGD’: 1.345223906, ‘THB’: 31.2702196492, ‘HRK’: 6.4451728248, ‘EUR’: 0.8513536523, ‘MYR’: 4.1454963392, ‘NOK’: 8.5482717521, ‘CNY’: 6.572024519, ‘BGN’: 1.6650774732, ‘PHP’: 48.5918610591, ‘PLN’: 3.9238038481, ‘ZAR’: 14.6495828367, ‘CAD’: 1.2588966457, ‘ISK’: 126.5962880981, ‘BRL’: 5.6316192746, ‘RON’: 4.1791248084, ‘NZD’: 1.4307849481, ‘TRY’: 8.1647369317, ‘JPY’: 110.7015154095, ‘RUB’: 76.2765196663, ‘KRW’: 1130.9041375788, ‘USD’: 1.0, ‘AUD’: 1.3195981611, ‘HUF’: 308.0538055508, ‘SEK’: 8.7479141836}
- 源码
from forex_python.converter import CurrencyRates
c = CurrencyRates()
amount = int(input("Enter the amount you want to convertn"))
from_currency = input("Fromn")
to_currency = input("Ton")
temp = c.convert(from_currency, to_currency, amount)
result = round(temp, 3)
print(f"{amount} {from_currency} = {result} {to_currency}")
- 输出样例
3.1 描述
创建一个程序来模拟掷骰子 当用户询问时,使用random模块生成一个1到6之间的数字(dice_emulator)。
3.2 python代码
import random
while int(input("Press 1 to roll the dice or 0 to exitn")):
print(random.randint(1, 6))
3.3 输出
4. 密码记录器
4.1 描述
应用太多了,密码都用一个,太不安全,制作一个脚本,存储密码。
4.2 代码
def write_password():
with open("passwords.txt", "a+") as f:
name = input("name:n")
f.write(f"{name} ")
count = input("count:n")
f.write(f"{count} ")
password = input("password:n")
f.write(f"{password} n")
def print_password():
with open("passwords.txt", "r+") as f:
print(f.read())
if __name__ == '__main__':
select = input("1:写 0:读n")
if select == 1:
write_password()
else:
print_password()
4.3 输出
5. 赫夫曼编码# huffman coding
# tree node type
class Node:
def __init__(self, weight):
self.left = None
self.right = None
self.parent = None
self.weight = weight
def is_left(self):
return self.parent.left == self
# create leaf nodes
def create_leaf_nodes(_char_weight):
return [Node(i) for i in _char_weight]
# create huffman tree by leaf nodes
def create_huffman_tree(_leaf_nodes):
queue = _leaf_nodes[:]
while len(queue) > 1:
queue.sort(key=lambda node: node.weight)
node_left = queue.pop(0)
node_right = queue.pop(0)
node_parent = Node(node_left.weight + node_right.weight)
node_parent.left = node_left
node_parent.right = node_right
node_left.parent = node_parent
node_right.parent = node_parent
queue.append(node_parent)
queue[0].parent = None
return queue[0]
# huffman encoding
def huffman_encoding(_leaf_nodes, _root):
codes = [''] * len(_leaf_nodes)
for i in range(len(_leaf_nodes)):
temp_node = _leaf_nodes[i]
while temp_node != _root:
if temp_node.is_left():
codes[i] = '0' + codes[i]
else:
codes[i] = '1' + codes[i]
temp_node = temp_node.parent
return codes
if __name__ == '__main__':
char_weight = [('C', 2), ('G', 2), ('E', 3), ('K', 3), ('B', 4),
('F', 4), ('I', 4), ('J', 4), ('D', 5), ('H', 6),
('N', 6), ('L', 7), ('M', 9), ('A', 10)]
leaf_nodes = create_leaf_nodes([item[1] for item in char_weight])
root = create_huffman_tree(leaf_nodes)
huffman_codes = huffman_encoding(leaf_nodes, root)
for item in zip(char_weight, huffman_codes):
print(f"<{item[0][0]} , {item[1]}>")



