jeudi 3 novembre 2016

Python flask linecache random line from text file doesn't work each time

Hi I'm trying do write simple word game for school project. I have a file with 235886 words in separate lines. When i refresh my web page most of the times random word is in html paragraph i want, but every few times it is not there. I'm a python beginner and I'm not very good with web development. Can you see any error in my code? is there any way to debug it?

from flask import Flask, render_template , request
import linecache , random
app = Flask(__name__)


def get_word() -> str:
   c = random.randrange(235886)
   p = linecache.getline('gameWords.txt' , c)

   return p


@app.route('/game')
def display_game() -> 'html':
    p = get_word()
    return render_template('game.html' , word = p)


if __name__ == '__main__':
    app.run(debug=True) 




Aucun commentaire:

Enregistrer un commentaire