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