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