카테고리 없음

스파르타 AI-8기 TIL (9/18)

kimjunki-8 2024. 9. 18. 21:26

Today,

I've made a lottery site(quite hard....)

From this, I was able to learn how to make a server. (flask, venv)

flask folder structure
- - -
flask
|— venv
|— app.py (server)
|— templates
         |— index.html (client file)

I could not understand the whole part, but I could get some ideas(like the frame itself?)

it was a great chance to get close to ChatGPT. 

from flask import Flask, render_template
import random

app = Flask(__name__)

def generate_lotto_numbers():
    lotto_numbers = random.sample(range(1, 46), 6)
    return sorted(lotto_numbers)

def count_common_elements(list1, list2):
    common_elements = set(list1) & set(list2)
    return len(common_elements)

@app.route('/')
def home():
    name = "Kevin"
    lotto_num = [15, 16, 18, 34, 29, 11]
    random_lotto = generate_lotto_numbers()
    common_count = count_common_elements(lotto_num, random_lotto)

    context = {
        "name": name,                
        "lotto_num": lotto_num,      
        'lotto_numbers': random_lotto,
        "common_count": common_count  
    }
   
    return render_template('index.html', data=context)

@app.route('/mypage)
def mypage():
    return 'This is My Page!'

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

First, make the random numbers by making a function (def generate_lotto_numbers():)

second, make a box that will work to make both random and written numbers, =

Third make variables to put the numbers to check the contrasts

    context = {
        "name": name,                
        "lotto_num": lotto_num,      
        'lotto_numbers': random_lotto,
        "common_count": common_count  
    }

These will be shown on the screen(both the results and numbers)
Good night ^^