题解 | #密码游戏#
密码游戏
https://www.nowcoder.com/practice/36641ab168664384aff798ba7ce34bc1
num = input()
new_num = [(int(i) + 3) % 9 for i in num]
new_num[0], new_num[2] = new_num[2], new_num[0]
new_num[1], new_num[3] = new_num[3], new_num[1]
for i in new_num:
print(i,end="")
mainly use the feature of list to solve this problem
# 其中 f 表示format
[f'ch_{i}' for i in range(10)]
# output
['ch_0', 'ch_1', 'ch_2', 'ch_3', 'ch_4', 'ch_5', 'ch_6', 'ch_7', 'ch_8', 'ch_9']
# 如果不加format
['ch_{i}' for i in range(10)]
['ch_{i}', 'ch_{i}', 'ch_{i}', 'ch_{i}', 'ch_{i}', 'ch_{i}', 'ch_{i}', 'ch_{i}', 'ch_{i}', 'ch_{i}']
查看2道真题和解析