Python core Programming chapter sixth Exercise 6-14

Source: Internet
Author: User

Random number. Design a "stone, scissors, cloth" game, sometimes called "Rochambeau", you may have played as a child, the following is the rule. You and your opponent, at the same time making specific gestures, must be one of the following: Stone, scissors, cloth. The winner is generated from the following rules, which are itself a paradox.
(a) cloth-clad stones.
(b) The stone hit the scissors.
(c) Scissors cut rags. In your computer version, the user enters his or her options, the computer is looking for a random option, and then your program decides on a winner or a tie. Note that the best algorithm is to use the IF statement as sparingly as possible.
Answer
The code is as follows:

#!/usr/bin/env python#-*-coding:utf-8-*-Import randomguess_list = [' Scissor ', ' rock ', ' paper ']computer_list = RANDOM.C Hoice (guess_list) guess = raw_input (' Please input a string (Scissor,rock,paper): ') win_list = [[' Scissor ', ' paper '],[' Rock ', ' scissor '],[' paper ', ' rock ']]failure_list = [[' Scissor ', ' Rock '],[' rock ', ' paper '],[' paper ', ' Scissor ']]if [ Guess,computer_list] in win_list:    print ' You win! ' elif [Guess,computer_list] in failure_list:    print ' You failure! ' else:    print ' Tie,try again '

  

Python core Programming chapter sixth Exercise 6-14

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.