Web Services (已解决-在第4个代码中) flask 从URL中取值,并将所有倍数相加(例如:4,最多50)=>(312)on website [duplicate]

46qrfjad  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(127)

此问题已存在

python flask input multiple value, how can we multiply or doing the calculation from the value we get from URL(how can i correct) [closed]
上个月关门了。
Python flask 从URL中取值,并将网站上的所有多个数字(例如:4,最多50)相加=〉(312),以及我如何才能做到这一点,以便修复。

# import main Flask class and request object
from flask import Flask, request
 
# create the Flask app
app = Flask(__name__)
 
@app.route('/data')
def query_example():

    num = request.args.get('num')

    count = request.args.get('count')

    num_int = int(num)
    count_int = int(count)

    i =1
    sum =0

    while i<=count_int:
       if i % num_int == 0:
          sum+= i
        
          i +=1
    ans = sum

    return ans
         


if __name__ == '__main__':
    # run app in debug mode on port 5000
    app.run(debug=True, port=5000)

code picture
desired URL(picture)例如:http://127.0.0.1:5000/data?num=3&count=50
4 up to 50 simple function(picture)

我更正的新代码(仍无法获得“sum2”结果)

# import main Flask class and request object
from re import I
from flask import Flask, request
 
# create the Flask app
app = Flask(__name__)


@app.route('/data')

def query_example():
    # if key doesn't exist, returns None
    name = request.args.get('name')
 
    # if key doesn't exist, returns a 400, bad request error
    num1 = request.args['num1']
    num2 = request.args['num2']
 
    
 
    # take input from string to other variable變數
 
    num1_int = int(num1)
    num2_int= int(num2)

    multiply = num1_int*num2_int

    i=1
    sum2=0


    for i in range (1,  num1_int,1 ) :
        if i % num2_int ==0:
         sum2+=i 
    i+=1

 
    return '''
              <h1>The name is: {}</h1>
              <h1>The num1  is: {}</h1>
              <h1>The num2  is: {}</h1>  
              <h1>The multiply is: {}</h1>
              <h1>The sum2  is: {}</h1>
              '''.format(name, num1_int,num2_int , multiply ,sum2)
             
 
if __name__ == '__main__':
    # run app in debug mode on port 5000
    app.run(debug=True, port=5000)

我现在确实更改了外部的i +=1,但新代码不起作用。

# import main Flask class and request object
from re import I
from flask import Flask, request
 
# create the Flask app
app = Flask(__name__)

@app.route('/data')

def query_example():
    # if key doesn't exist, returns None
    name = request.args.get('name')
 
    # if key doesn't exist, returns a 400, bad request error
    num1 = request.args['num1']
    num2 = request.args['num2']
 
    
 
    # take input from string to other variable變數
 
    num1_int = int(num1)
    num2_int= int(num2)

    multiply = num1_int*num2_int

    i=1
    sum2=0

    while i<=num2_int:
        if i % num1_int ==0:
            sum+=i 

        i+=1

 
    return '''
              <h1>The name is: {}</h1>
              <h1>The num1  is: {}</h1>
              <h1>The num2  is: {}</h1>  
              <h1>The multiply is: {}</h1>
              <h1>The sum2  is: {}</h1>
              '''.format(name, num1_int,num2_int , multiply ,sum2)
             
 
if __name__ == '__main__':
    # run app in debug mode on port 5000
    app.run(debug=True, port=5000)

“第四个密码,解出来的那个”

from flask import Flask, request
app = Flask(__name__)
 
@app.route('/data')
def query_example():
    # if key doesn't exist, returns None
    name = request.args.get('name')
 
    num1 = request.args['num1']
    num2 = request.args['num2']
 
    num1_float = int(num1)
    num2_float = int(num2)
    multiply = num1_float*num2_float
 
    i=1
    sum2=0
    while i< num1_float:
        if i % num2_float ==0:
            sum2+=i
        i+=1
    aaa =sum2
    return '''
              <h1>The name is: {}</h1>
              <h1>The num1  is: {}</h1>
              <h1>The num2  is: {}</h1>  
              <h1>The multiply is: {}</h1>
              <h1>The sum2  is: {}</h1>
              '''.format(name, num1_float,num2_float , multiply ,aaa)
             
if __name__ == '__main__':
    # run app in debug mode on port 5000
    app.run(debug=True, port=5000)

DEMO已解决的代码图片(将所有2的倍数相加,直到20

ffscu2ro

ffscu2ro1#

我复制了第一个代码并尝试修改它。它的工作原理很好,但不确定它是否是你所期望的。代码如下:

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route('/data')
def query_example():
    print('Loading data...\n')

    num = request.args.get('num')
    count = request.args.get('count')

    num_int = int(num)
    count_int = int(count)

    print(f'num_int: {num_int}')
    print(f'count_int: {count_int}\n')

    i = 1
    sum = 0

    while i <= count_int:
        if i % num_int == 0:
            sum += i
            
        i += 1

    return render_template('output.html', result=sum)

这是来自URL http://localhost:5000/data?num=3&count=50的结果。

[编辑]
您将i += 1错放在while内。它应该在if外,因为第一次迭代从未满足该条件。

相关问题