【问题标题】:web app not doing substraction operation-PythonWeb应用程序不做减法运算-Python
【发布时间】:2017-12-29 00:12:54
【问题描述】:

我正在尝试创建一个 Web 应用程序,其中一个功能是添加、减去和乘以数字,这些数字实现为用空格分隔的列表,它们都可以工作,但减法会给我带来奇怪的结果,例如,如果我尝试输入 6 2,预期的结果可能是 4,但它现在给了我 -2,我相信这是因为它从索引 0 中减去所以它只给出第二个数字,所以我改变了它到 int(form_text[0]) 但现在它给了我 IndexError: string index out of range 并且我确定我输入了两个数字。

@app.route('/add_numbers', methods=['GET', 'POST'])
def add_numbers_post():
# --> ['5', '6', '8']
# print(type(request.form['text']))
if request.method == 'GET':
    return render_template('add_numbers.html')

elif request.method == 'POST':
    form_text = request.form['text'].split()
    print(request.form['text'].split())
suma_total = 0
resta_total = int(form_text[0])
multiplicacion_total = 1
try:
    for str_num in form_text:
        suma_total += int(str_num)
        resta_total -= int(str_num[1])
        multiplicacion_total *= int(str_num)
    return render_template('add_numbers.html', result_suma=str(suma_total), result_multiplicacion=str(multiplicacion_total), result_resta=str(resta_total))
except ValueError:
    return "Easy now! Let's keep it simple! 2 numbers with a space between them please"

【问题讨论】:

  • 如果你在每次迭代中从 0 中减去,它总是一个负值。即: (init) -> resta_total == 0,resta_total -= 6 -> resta_total == 6,resta_total -= 2 -> resta_total == 8

标签: python html css flask


【解决方案1】:

发生这种情况是因为您要从 resta_total 中减去“for str_num in request.form["text"].split()”中的所有数字,即 0。resta_total 变量应设置为 request.form 中的第一个 int [“文本”].split()。试试这个...

@app.route('/add_numbers', methods=['GET', 'POST'])
def add_numbers_post():
# --> ['5', '6', '8']
# print(type(request.form['text']))
form_text = []
if request.method == 'GET':
    return render_template('add_numbers.html')

elif request.method == 'POST':
    form_text = request.form['text'].split()
    print(request.form['text'].split())
suma_total = 0
resta_total = int(form_text[0]) - int(form_text[1])
multiplicacion_total = 1
try:
    for str_num in request.form['text'].split():
        suma_total += int(str_num)
        multiplicacion_total *= int(str_num)
    return render_template('add_numbers.html', result_suma=str(suma_total), 
result_multiplicacion=str(multiplicacion_total),result_resta=str(resta_total))
except ValueError:
    return "Easy now! Let's keep it simple! 2 numbers with a space between them please"

请注意,这假设 form_text[0] 有一个值。您也可以将 for 循环更改为“for str_num in form_text”。

处理两个以上整数的减法...

idx = 0
for str_num in form_text:
    suma_total += int(str_num)
    multiplication_total *= int(str_num)
    if idx > 0:
        resta_total -= int(str_num)
    else:
        resta_total = int(str_num)
    idx += 1

【讨论】:

  • meop2664 感谢您的回复,我尝试了它,但它给了我一个错误,所以我稍微调整了一下,然后它给了我 IndexError: string index out of range 检查我编辑的帖子并进行了更改。
  • @AllanReyes 你不能做 str_num[1]。这是试图通过来自“6”或“2”的form_text 中的字符串进行索引。如果输入始终是两个数字,您可以通过我上面的编辑来实现减法部分。查看“resta_total”实例化。如果您希望一次能够输入两个以上的整数,则需要更改 for 循环以跟踪您所在的索引。我将编辑我的帖子以显示如何做到这一点。
猜你喜欢
  • 1970-01-01
  • 2011-03-26
  • 2020-12-06
  • 1970-01-01
  • 2011-03-24
  • 2013-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多