我正在使用Python 3.6编写数字根程序。通常会找到特定数字的数字根。
这是代码
def digital_root():
n = input("Enter the number:")
sum = 0
integer = int(n)
if len(n) == 1:
print("The digital root of " + n + " is: " + n)
else:
for x in n:
sum += int(x)
return sum
print(digital_root())
当我的输入为38时,输出为11,而应为2。
答案 0 :(得分:0)
您必须继续执行该过程,直到获得长度1。可以编写递归函数来做到这一点。
var express = require('express');
var app = express();
var request=require("request");
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,clientSecret");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,PATCH,DELETE,OPTIONS');
next();
});
app.post("/push/v1/apps/:appID/devices",function(req,response){
var appID=req.params.appID;
var options={
url:"https://pushapp.sampleapp.net/push/v1/apps/"+appID+"/devices",
headers:{
'Content-Type':req.headers["content-type"],
'clientSecret':req.headers["clientsecret"]
},
body: JSON.stringify(req.body)
}
request.post(options,function(err,res,body){
if(res.statusCode==201){
response.sendStatus(201).json(JSON.parse(body));
}
else{
response.sendStatus(res.statusCode);
}
});
});
答案 1 :(得分:0)
您将需要编写一个循环来进行求和,直到只剩下一个数字为止。
示例:
def digital_root():
n = input("Enter the number:")
sum = 0
integer = int(n)
calculate = True
while calculate:
if len(n) == 1:
print("The digital root of " + str(integer) + " is: " + n)
calculate = False
else:
sum = 0
for x in n:
sum += int(x)
n = str(sum)
return sum
print(digital_root())