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 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值