题解 | 密码游戏
# 接收四位数输入 n = int(input()) # 将整数转化为字符串以便逐位操作 n_str = str(n) # 保证输入是四位数 if len(n_str) != 4: raise ValueError() # 定义一个列表来存储每位处理后的数字 new_digits = [] # 对每位数字进行处理 for digit in n_str: new_digit = (int(digit) + 3) % 9 new_digits.append(new_digit) # 交换第1位和第3位,第2位和第4位 new_digits[0], new_digits[2] = new_digits[2], new_digits[0] new_digits[1], new_digits[3] = new_digits[3], new_digits[1] # 将数字重新转化为字符串,确保是四位数格式 result = ''.join(str(digit) for digit in new_digits) print(result)