python剪刀石头布

初步实现:

import random

list_guess = ['rock', 'scissors', 'paper']
player = input ('type your choice:')
compter = random. choice (list_guess)

while True :
    print ('player is :'+ player)
    print ('compter is :'+ comp)
    if player not in list_guess:
        print ("重新输入")
    elif player in compter:
        print ("平局!")
    elif player is 'rock' and compter is 'scissors' or 'paper' or player is 'scissors' and compter is 'paper':
        print ("电脑赢了!")
    elif player is 'paper' and compter is 'scissors' or 'rock':
        print ("你赢了!")
#     elif player is 'scissors' and compter is 'paper':
#         print ("电脑赢了!")  
    break 

进阶:

import random

list_guess = {0 :'rock',1 : 'scissors', 2 :'paper'}

while True :
    player = int(input('请输入 0剪刀 1石头 2布:'))
    computer = random. randint (0, 2)
    print ("player is :" + list_guess [player])
    print ("computer is :" + list_guess [computer])
    
    if player not in range (0,3):
        print ("重新输入")
    elif player == computer:
        print ("平局!")
#     elif player is 'rock' and computer is 'scissors' or 'paper' or player is 'scissors' and computer is 'paper':
    elif (player- computer == 1 or player - computer ==-2 ):
        print ("你赢了!")
    else:
        print ("电脑赢了!")
    break 

 

你可能感兴趣的:(python)