Rock-paper-scissors with Python Let's run on a Windows local server for beginners

This article is for Django Girls Japan Python beginners, This is a study session material for "Let's make a rock-paper-scissors game with Python". Since the author is also a beginner, we apologize for any inconvenience. Namer environment: Windows 10 python3.5

CGI server

With the http.server module, you can write a program written in Python in a web server. It is possible to execute. You can easily create a web application without preparing a web server or setting up moromoro. You can try it.

This time Janken Poi in Python Based on the rock-paper-scissors game created in, create something that runs on the browser.

The structure of the file is as follows.

kaisou.JPG

While in the janken folder at the command prompt python -m http.server --cgi And press the enter key to start the local server. (For Python2, enter python -m CGIHTTPServer.)

cgi.JPG

Now you can run the game on your browser. janken3.JPG

HTML source

This is the html source placed directly under the janken folder.

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"> </head>
<body>
  <center><br><br>
  <form action="/cgi-bin/janken.py" method="POST"><h1><font color="#FF7F50">Janken</font></h1><br /><br />
    <font size="5"><input type="radio" name="janken" value=1>Goo<br />
    <input type="radio" name="janken" value=2>Choki<br />
    <input type="radio" name="janken" value=3>Par<br /><br /></font>
    <input type="submit" name="submit" />
  </form>
  </center>
</body>
</html>

  <form action="/cgi-bin/janken.py" method="POST">

Here you can pass requests from this page to janken.py under the cgi-bin folder. (Details of the html source are omitted here.)

Python file

The source for janken.py under the janken / cgi-bin folder.

# -*- coding: utf-8 -*-
# !/usr/bin/env python

import cgi
import random

form = cgi.FieldStorage()

dic = {"1": "Goo", "2": "Choki", "3": "Par"}

user = form.getfirst('janken')
user_choice = dic[user]

choice_list = ["1", "'2", "3"]
pc = dic[random.choice(choice_list)]

draw = '<font color="#32CD32">DRAW</font>'
win = '<font color="#FF7F50">You Win!!</font>'
lose = '<font color="#0000FF">You lose!!</font>'

if user_choice == pc:
    judge = draw
else:
    if user_choice == "Goo":
        if pc == "Choki":
            judge = win
        else:
            judge = lose

    elif user_choice == "Choki":
        if pc == "Par":
            judge = win
        else:
            judge = lose

    else:
        if pc == u"Goo":
            judge = win
        else:
            judge = lose


html_body = """
<html><body><center><br><br><br>
What you chose%s<br><br>
The computer chose%s<br><br>
<font size="5">Result is%s </font><br><br>
<a Href ="http://127.0.0.1:8000/janken.html">Return</a>
</center></body></html>""" % (user_choice, pc, judge)

print("Content-type: text/html\n")
print(html_body)

Recommended Posts

Rock-paper-scissors with Python Let's run on a Windows local server for beginners
Run servo with Python on ESP32 (Windows)
Installing TensorFlow on Windows Easy for Python beginners
Local server with python
Put Docker in Windows Home and run a simple web server with Python
Until you install Python with pythonbrew and run Flask on a WSGI server
Build a local development environment with WSL + Docker Desktop for Windows + docker-lambda + Python
Build a Python extension for E-Cell 4 on Windows 7 (64bit)
PIL with Python on Windows 8 (for Google App Engine)
Procedure for building a CDK environment on Windows (Python)
How to run Django on IIS on a Windows server
Run Openpose on Python (Windows)
Run python with PyCharm (Windows)
Let's run Excel with Python
Until you create a machine learning environment with Python on Windows 7 and run it
Let's get started with Python ~ Building an environment on Windows 10 ~
Build Python3 for Windows 10 on ARM with Visual Studio 2019 (x86) on Windows 10 on ARM
Library for specifying a name server and dig with python
Build a python environment on CentOS 7.7 for your home server
Run a Linux server on GCP
Run SwitchBot on Windows 10 with Bleak
Run TensorFlow2 on a VPS server
Let's make a graph with python! !!
Run unittests in Python (for beginners)
Getting started with Python 3.8 on Windows
Create a Python environment for professionals in VS Code on Windows
Create a Python execution environment for Windows with VScode + Remote WSL
Build a 64-bit Python 2.7 environment with TDM-GCC and MinGW-w64 on Windows 7
Notes for using OpenCV on Windows10 Python 3.8.3.
INSERT into MySQL with Python [For beginners]
Let's make a shiritori game with Python
Programming environment for beginners made on Windows
Run a local script on a remote host
Let's create a virtual environment for Python
Let's create a free group with Python
Django + Apache with mod_wsgi on Windows Server 2016
A memo with Python2.7 and Python3 on CentOS
Building a Python 3.6 environment with Windows + PowerShell
Map rent information on a map with python
Let's put together Python for super beginners
Creating a python virtual environment on Windows
Let's make a voice slowly with Python
[Python] Read images with OpenCV (for beginners)
WebApi creation with Python (CRUD creation) For beginners
Run a Python web application with Docker
Let's make a web framework with Python! (1)
Let's make a Twitter Bot with Python!
Run Jupyter notebook on a remote server
Run matplotlib on a Windows Docker container
Let's make a web framework with Python! (2)
[For beginners] Try web scraping with Python
A textbook for beginners made by Python beginners
Run python wsgi server on NGINX Unit
I tried to refer to the fun rock-paper-scissors poi for beginners with Python
Steps to create a Python virtual environment with VS Code on Windows
Put MeCab binding for Python with pip on Windows, mac and Linux
A python script for Mac that zips without garbled characters on Windows
Build a local server with a single command [Mac]
Periodically run a python program on AWS Lambda
Install and run Python3.5 + NumPy + SciPy on Windows 10
Put MicroPython on Windows to run ESP32 on Python