我是否遗漏了缩进或者其他问题?

时间:2018-02-10 19:47:49

标签: python

add-lines

我试图创建一个猜谜游戏瓶页面。 继续回来"文件" guess.py",第27行会议['颜色'] ='红色' ^ IndentationError:unindent与任何外部缩进级别不匹配

2 个答案:

答案 0 :(得分:1)

即使代码在语法上似乎是正确的,它看起来好像是这部分:

@app.route('/execute', methods=['POST'])
def check_guess():
    session(['guess']) = int(request.form['guess'])
if (session['guess']==session['num']):
    session['message'] = str(session['num'])+'was the number!'
    session['color']='green'
elif int(request.form['guess']) < session['num']:
    session['message'] = 'Too low.'
    session['color']='red'
else: 
    session['message'] = 'Too high.'
    session['color']='red'
    return redirect('/')

可能是这样的:

@app.route('/execute', methods=['POST'])
def check_guess():
    session(['guess']) = int(request.form['guess'])
    if (session['guess']==session['num']):
        session['message'] = str(session['num'])+'was the number!'
        session['color']='green'
    elif int(request.form['guess']) < session['num']:
        session['message'] = 'Too low.'
        session['color']='red'
    else: 
        session['message'] = 'Too high.'
        session['color']='red'
    return redirect('/')

另外请确保不要混合标签和空格。如果使用空格进行缩进,请确保代码中没有任何制表符。

答案 1 :(得分:0)

您的代码没有正确缩进,请尝试下面的代码

from flask import Flask, render_template, request, redirect, session
import random
app = Flask(__name__)
app.secret_key = "guess_secret"

@app.route('/') 
def landing_page():
    if 'num' not in session:
        session['num'] = random.randrange(0,101)
    if 'message' not in session:
        session['message'] = ''
    if 'color' not in session:
        session['color'] = ''
    return render_template('indexa.html', msg = session['message'], color = session['color'])

@app.route('/execute', methods=['POST'])
def check_guess():
    session(['guess']) = int(request.form['guess'])
if (session['guess']==session['num']):
    session['message'] = str(session['num'])+'was the number!'
    session['color']='green'
elif int(request.form['guess']) < session['num']:
    session['message'] = 'Too low.'
    session['color']='red'
else: 
    session['message'] = 'Too high.'
    session['color']='red'
    return redirect('/')

@app.route('/reset', methods=['POST'])
def reset():
    session.pop('number') 
    session.pop('guess')
    session.pop('color')
    return redirect('/')

app.run(debug=True)