랜덤 로또 당첨기

chanloper·2024년 6월 24일

python

목록 보기
4/12

import random
app = Flask(__name__)
@app.route('/')
def home(): 
    name = '윤성'
    lotto = [16,18,22,43,32,11]

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

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

    context = {
        "name": name,
        "lotto": lotto,
        "random_lotto" : random_lotto,
        "common_count" : common_count,
    }

    return render_template('index.html', data=context) 
    
  if __name__ == '__main__':  
  	app.run(debug=True)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            text-align: center;
            font-family: Arial,sans-serif;
        }

        .lotto-numbers {
            list-style: none;
            padding: 0;
        }

        .lotto-numbers li {
            display: inline-block;
            margin : 5px;
            border-radius: 50%;
            font-weight: bold;
            padding: 10px;
            background-color: #FFFF00;
        }
        .randomlotto-numbers {
            list-style: none;
            padding: 0;
        }

        .randomlotto-numbers li {
            display: inline-block;
            margin : 5px;
            border-radius: 50%;
            font-weight: bold;
            padding: 10px;
            background-color: blue;
        }
        img {
            height:100px;
        }
    </style>
</head>
<body>
    <h1>안녕, {{ data.name }}</h1>
    <img src="{{url_for('static', filename= 'coinman.png')}}" alt="">
    <h2>로또 번호 : {{ data.lotto }}</h2>
    <h2>랜덤 로또 번호 : {{data.random_lotto}}</h2>
    <ul class="lotto-numbers">
        {% for element in data.lotto %}
        <li>{{ element|e }}</li>     
        {% endfor %}

    </ul>
    <ul class="randomlotto-numbers">
        {% for element in data.lotto %}
        <li>{{ element|e }}</li>     
        {% endfor %}
    </ul>

    <!-- 당첨 개수에 따라 메세지 표시-->
    {% if data.common_count == 6 %}
        <h2>{{data.common_count}}개 맞았습니다. 1등입니다!</h2>
    {% elif data.common_count == 5 %}
        <h2>{{data.common_count}}개 맞았습니다. 2등입니다!</h2>
    {% elif data.common_count == 4 %}
        <h2>{{data.common_count}}개 맞았습니다. 3등입니다!</h2>
    {% elif data.common_count == 3 %}
        <h2>{{data.common_count}}개 맞았습니다. 4등입니다!</h2>
    {% else %}
        <h2>{{data.common_count}}개 맞았습니다. 탈락입니다.</h2>
    {% endif %}
    
</body>
</html>

서버 구현 후, Flask를 이용하여 app.py 와 index.html 연동

profile
이것 뭐에요?

0개의 댓글