gpt4 book ai didi

python - 为什么这个数字根功能不起作用?

转载 作者:太空宇宙 更新时间:2023-11-04 09:44:39 25 4
gpt4 key购买 nike

<分区>

我在做a challenge on codewars ,我应该用 Python 编写一个数字根函数。我不知道为什么它不起作用。这是我第一次尝试递归。我真的很想全神贯注于此。

def digital_root(num):
#Check to see if num has more than one digit
if num > 9:
x = 0
z = 1
#Create an array of the digits in num
digits = [int(d) for d in str(num)]

#Convert string elements in digits[] to ints
for n in digits:
digits[x] = int(x)
x = x + 1

#Add each element in digits[] to digits[0]
for n in digits:
digits[0] = digits[0] + digits[z]

#If digits[0] has more than one digit, then run digital_root with digits[0] in the parameters
if digits[0] > 9:
digital_root(digits[0])
else:
return digits[0]
else:
return num

digital_root(15)


>> Program finished with exit code 0

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com