用switch。。case语句输入1任意个x 尾数是0直接输出尾数是12x+1输出;尾数是22x+2输出尾数是对象32x+3输出依次类推尾号是92x+9输出
以下是使用switch...case语句实现的代码:
x = int(input("请输入x的值:"))
last_digit = x % 10
switcher = {
0: "尾数是0",
1: "尾数是1,输出:{}".format(2*x+1),
2: "尾数是2,输出:{}".format(2*x+2),
3: "尾数是3,输出:{}".format(2*x+3),
4: "尾数是4,输出:{}".format(2*x+4),
5: "尾数是5,输出:{}".format(2*x+5),
6: "尾数是6,输出:{}".format(2*x+6),
7: "尾数是7,输出:{}".format(2*x+7),
8: "尾数是8,输出:{}".format(2*x+8),
9: "尾数是9,输出:{}".format(2*x+9)
}
output = switcher.get(last_digit, "输入的x不合法")
print(output)
在代码中,首先获取输入的x值,并计算x的尾数(即x % 10)。然后使用switcher字典来定义每个尾数对应的输出内容。最后根据输入的尾数获取相应的输出内容并打印出来。如果输入的x不在0到9之间,则输出"输入的x不合法"。
原文地址: https://www.cveoy.top/t/topic/i7o5 著作权归作者所有。请勿转载和采集!