Learn to write: Python implements scissors, stone, cloth games

Source: Internet
Author: User

Objective:

This script implements the game of scissors and stone cloth, but there is no exception handling. Only user input 0, 1, 2 o'clock can be normal processing.

#!/usr/bin/env python
# Coding:utf8
Import Random//Imports random module, which is a module that generates random numbers

All_choice = ["Stone", "scissors", "cloth"]//define a list and add the selection to the list
Win_list = [["Stone", "Scissors"],["scissors", "cloth"],["cloth", "stone"]]//Define a user-won list, the elements in the list are still a list

prompt = "" "(0) stone//define a variable and write the prompt to this variable
(1) Scissors
(2) Cloth
Please select (0/1/2): "" "

IND = Int (raw_input (prompt))//Because a string type is entered, the string is converted to an integer type
Player = All_choice[ind]//list can be removed from the list of values in the corresponding index
Computer = Random.choice (all_choice)//random selection of a value

Print "Your choice:%s, Computer choice:%s"% (player, computer)//prompt information, friendly interface
If player = = computer://If the values of these two variables are equal
Print "\033[32;43;1m draw \033[0m"//Output Draw
elif [player, computer] in Win_list://If the list is in Win_list
print "\033[31;45;1myou Win!!! \033[0m "//Output you won
ELSE://otherwise
print "\033[31;46;1myou lose!!! \033[0m "//Just out you lose

Note: \00[30+ is the foreground color, 40+ is the background color, \033 0m is turned off the colour end 1m is the aggravating display

This article is from the "Court of the Odd Tree" blog, please be sure to keep this source http://zhangdl.blog.51cto.com/11050780/1827486

Writing: Python Implements scissors, stone, cloth games

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.