Python implementation of stone scissors code sharing

  • 2020-04-02 13:57:25
  • OfStack

I wrote an article before about JS based rock scissors program, and today I wrote an example based on Python, where the algorithm is a little special but I can't think of a good algorithm rule at the moment.

Code:


# encoding=UTF-8
# Stone scissors The program
# sasihorfswe
import random
 
# Define the stone scissors dictionary
dict = {1:' scissors ',2:' stone ',3:' cloth '}
 
for row in dict:
    print ' Serial number :',row,' = ',dict[row]
 
print ' What's the matter with you? '
 
loop = True
while loop:
    you = raw_input(' Please enter the number enter : ')
    try:
        you = int(you)
        if you>=1 and you<=3:
            loop = False
        else:
            print ' Please enter the 1-3 Range number '
    except Exception,e:
        print ' Please enter the correct number '
 
dn = random.randint(1,3)
print ' You: ',dict[you]
print ' Computer: ',dict[dn]
print ' Results: ',
 
if dn==you:
    print ' A draw '
elif (you>dn and you-dn==1) or you+2==dn:
    print ' You win '
else:
    print ' The computer games '


Related articles: