用for loop回答下面问题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 08:02:57 浏览: 81
Sure, here is the modified `proddigits()` function that uses a `for` loop to calculate the product of digits:
```python
def proddigits(n):
# Initialize the result
result = 1
# Convert the number to a string for iteration
n_str = str(n)
# Loop through each digit of the number
for digit in n_str:
# Convert the digit back to an integer
digit_int = int(digit)
# Multiply it to the result
result *= digit_int
# Return the final result
return result
```
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.
阅读全文