Share the code of the stone scissors provided by Python and the python stone scissors

Source: Internet
Author: User

Share the code of the stone scissors provided by Python and the python stone scissors

I have previously written an article about the JS-based stone scissors cloth program "share the source code of the stone scissors cloth game implemented by JavaScript". Today I wrote an example based on Python, the algorithm here is a bit special, but I cannot think of a good algorithm law at the moment.

Code:
Copy codeThe Code is as follows:
# Encoding = UTF-8
# Stone scissors Program
# Li Zhong
Import random
 
# Define the stone scissors dictionary
Dict = {1: 'scissors ', 2: 'stone', 3: 'cloth '}
 
For row in dict:
Print 'Number: ', row,' = ', dict [row]
 
Print 'What do you get? '
 
Loop = True
While loop:
You = raw_input ('Enter the number and press Enter :')
Try:
You = int (you)
If you> = 1 and you <= 3:
Loop = False
Else:
Print 'Enter the number in the range of 1-3'
Except t Exception, e:
Print 'Enter the correct digit number'
 
Dn = random. randint (1, 3)
Print 'out: ', dict [you]
Print 'computer output: ', dict [dn]
Print 'result :',
 
If dn = you:
Print 'draw'
Elif (you> dn and you-dn = 1) or you + 2 = dn:
Print 'win out'
Else:
Print 'computer wins'


Code for a python scissors slate

This is not going through uli.
 
Python stone scissors game

#-*-Coding: UTF-8 -*-
# Stone scissors cloth
# Stone = 1 cloth = 2 scissors = 3
Import random

Class game:
Def _ init _ (self ):
Self. player = 1
Self. comp = 1
Self. label = [u "Stone", u "cloth", u "Scissors"]
Pass
Def run (self ):
While (True ):
Self. player = self. player_input ()
Self. comp = self. comp_input ()
Print "player % s" % (self. label [self. player-1])
Print "computer % s" % (self. label [self. comp-1])
Self. iswin (self. player, self. comp)
Pass
Def iswin (self, player, comp ):
If player = comp:
Print "Tie"
Return
Elif player> comp:
If player = 3 and comp = 1:
Print "you lose"
Return
Else:
Print "you win"
Return
Else:
If player = 1 and comp = 3:
Print "you win"
Return
Else:
Print "you lose"
Return

Def player_input (self ):
While (1 ):
Try:
Player = int (raw_input ("Input 1 or 2 or 3: \ n "))
If player> = 1 and player <= 3:
Return player
Except t:
Print "Input Error"

Def comp_input (self ):
Return random. randint (1, 3)
If _ name __= = "_ main __":
Newgame = game ()
Newgame. run ()

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.