0 votes
1 view
in Python by (50.5k points)

I was understanding a theory(guess a number game) from a book called Automate the Boring Stuff With Python. I got it to work using a command prompt but I'm admiring how I could arrange it up to execute on an html page. For this, I want to work with Flask and just execute the html pages locally for now.

import random

correct = random.randint(1,20)

print('I am thinking of a number between 1 and 20.')

for guessesTaken in range(1,6):

    print('Take a guess.')

    guess = int(input())

    if guess < correct:

        print('Your number is too low')

    elif guess > correct:

        print('Your number is too high')

    else:

        break

if guess == correct:

    print('Good work! You got the number in '+ str(guessesTaken)+ ' guesses')

else:

    print('Nope. The number I was thinking of was ' + str(correct))

1 Answer

0 votes
by (108k points)

First of all, you need to generate a folder with one file labeled app.py and one folder labeled templates containing a.html

1) app.py

from flask import Flask

from flask import request

from flask import render_template

import random

correct = random.randint(1,20)

count=0

app = Flask(__name__)

@app.route('/new')

def my_form():

    return render_template("a.html") # this should be the name of your html file

@app.route('/new', methods=['POST'])

def my_form_post():

    global correct

    global count

    msg = ''

    print(count)

    if count<6:

        count+=1

        text1 = request.form['text1']

        text1 = int(text1)

        if text1 < correct:

            msg = 'Your number is too low'

            return render_template("a.html", msg=msg)

        elif text1 > correct:

            msg = 'Your number is too high'

            return render_template("a.html", msg=msg)

        else:

            if text1 == correct:

                msg = 'Good work! You got the number in '+ str(count)+ ' guesses'

                count = 0

                correct = random.randint(1,20)

                return render_template("a.html", msg=msg)

    else:

        num = 'Nope. The number I was thinking of was ' + str(correct)

        correct = random.randint(1,20)

        msg = ''

        count=0

        return render_template("a.html", num=num)

if __name__ == '__main__':

    app.run(debug=True)

2) a.html

<!DOCTYPE html>

<html lang="en">

<body>

    <h1>I am thinking of a number between 1 and 20.</h1>

    <form action="/new" method="POST">

        <input type="text" name="text1">

        <input type="submit" name="my-form" value="Check !">

    </form>

    <h1>{{ msg }}</h1>

    <h1>{{ num }}</h1>

</body>

</html>

 If you want to know more about Python basics then do refer to the below Python tutorial that will help you out in a better way:

Related questions

0 votes
1 answer
asked Feb 8 in Python by ashely (50.5k points)
0 votes
1 answer
0 votes
1 answer
asked Sep 5, 2019 in Web Technology by Sammy (47.8k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...