用python写一个程序,输入一个三位整数,分别输出他的个位,十位和百位数字
时间: 2024-05-27 14:11:10 浏览: 72
num = int(input("请输入一个三位整数:"))
if num < 100 or num > 999:
print("输入错误,请重新输入")
else:
a = num // 100 # 百位数字
b = num % 100 // 10 # 十位数字
c = num % 10 # 个位数字
print("百位数字为:", a)
print("十位数字为:", b)
print("个位数字为:", c)
相关问题
用Python写下列程序,请输入一个三位整数125百位数字是一个位数字是5,十位数字是2,3位数字之和为8。
以下是 Python 代码:
num = 0
for i in range(1, 10):
for j in range(0, 10):
for k in range(0, 10):
if i == 1 and j == 2 and k == 5:
continue
if i == 1 and j == 5 and k == 2:
continue
if i == 2 and j == 1 and k == 5:
continue
if i == 2 and j == 5 and k == 1:
continue
if i == 5 and j == 1 and k == 2:
continue
if i == 5 and j == 2 and k == 1:
continue
if i + j + k == 8:
num += 1
print(num, ':', i, j, k)
用python输入一个三位整数,分别输出其个位,十位和百位
在Python中,你可以将输入的三位整数转换为字符串,然后通过切片操作来分别获取个位、十位和百位的数字。下面是一个示例函数:
```python
def split_three_digit_number(num):
str_num = str(num) # 转换为字符串
last_digit = str_num[-1] # 个位
tenth_digit = str_num[-2] if len(str_num) > 1 else '0' # 十位(如果不足三位,用0填充)
hundredth_digit = str_num[:-2] if len(str_num) > 2 else '00' # 百位(如果不足三位,用00填充)
return last_digit, tenth_digit, hundredth_digit
# 输入一个三位整数
three_digit_input = int(input("请输入一个三位整数:"))
last, tenth, hundred = split_three_digit_number(three_digit_input)
print(f"个位是:{last}\n十位是:{tenth}\n百位是:{hundred}")
阅读全文