(Solved- in 4th code) flask taking value from URL and sum all the multiple number (ex:4 , up to 50)=>(312) on website

Viewed 56

python flask taking value from URL and sum all the multiple number (ex:4 , up to 50)=>(312) on website, and how can I do it in order to fix.

# 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) ex:http://127.0.0.1:5000/data?num=3&count=50

4 up to 50 simple function(picture)

the new code I correcd(still can't get the "sum2" outcome)

# 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 do changed the i +=1 outside the while now, but the new code is not working.

# 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)

4th code, The sloved one

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 Solved code picture(add up all 2's multile till 20 DEMO Solved code picture(add up all 2's multile till 20)

1 Answers

I duplicated the first code and tried to modify it. It works like a charm, but not sure if it is what you'd expected. This is the code:

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)

This is the result from URL http://localhost:5000/data?num=3&count=50. Result

[EDIT]
You misplaced the i += 1 inside the while. It should be outside the if because the first iteration never satisfied the condition.

Related