Vi 编写一个剪刀石头布游戏

Code:

import random
print("不想玩游戏可以按q退出哦!")
while True:
    player = input("请输入 0剪刀 1石头 2布:")
    computer = random.randint(0,2)
    if player == "q":
        print("Bye!")
        break
    else:
        if player.isdigit():
            player = int(player)
            if player <=2 and player>=0: 
                #2.判断用户的输入,然后显示对应的结果
                if (player==0 and computer==2) or (player==1 and computer==0) or (player==2 and computer==1) :
                    print("赢了!可以买奶粉了!")
                elif player == computer:
                    print("平局了...洗洗手决战到天亮...")
                else:
                    print("输了!回家拿钱,再来!")
            else:
                print("输入的数字超过范围!")
                continue
        else:
             print("输入错误!")
             continue

感想:

一个简单的石头剪刀布游戏,一开始还没有判断输入错误问题,比如一开始用户不输入整数还有当输入整数的时候判断范围。通过加if判断语句排除用户输入错误来完善这个小游戏。有兴趣的可以拿去玩一下放松放松★

转载于:https://www.cnblogs.com/pjjo/p/7519438.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值