Write a python function proddigits() that inputs a positive integer number and returns the product of digits of that number. For example, if the input is 422, the function should return 16 (which is equal to 4×2×2 ).
时间: 2024-01-27 12:02:57 浏览: 63
python实现将字符串中的数字提取出来然后求和
Certainly! Here is a Python function `proddigits()` that takes a positive integer as input and returns the product of its digits:
```python
def proddigits(num):
# Initialize the product to 1
product = 1
# Convert the integer to a string
num_str = str(num)
# Iterate over each character in the string
for char in num_str:
# Convert the character back to an integer
digit = int(char)
# Multiply the digit to the product
product *= digit
# Return the final product
return product
```
You can call this function with a positive integer argument to get the product of its digits. For example:
```python
# Test the function
num = 422
print(proddigits(num)) # Output: 16
```
I hope this helps! Let me know if you have any other questions.
阅读全文