qq_37044185
努力的小bai
采纳率100%
2018-10-12 10:59

简单的python程序报错,改了半天也不知道哪里错了,请解答 谢谢

已采纳
play = input("请输入剪刀(0)石头(1)布(2):")

player = int(play)

com = 1

if (player==0 and com==2) or (player==1 and com==0) or (player==2 and com==1):
        print("你赢了,真厉害")
elif player==com:
        print("平局,再来一次")
else (player==0 and com==1) or (player==1 and com==2) or (player==2 and com==0):
        print("你输了,不要走,决战到天亮")

报错信息如下: File "jsb.py", line 9
else (player==0 and com==1) or (player==1 and com==2) or (player==2 and co
^
m==0):

SyntaxError: invalid syntax

网上查询说是因为括号、冒号什么的,我都用英文输入法重新打了一次还是错误,希望大家解答,初学者请大家多多理解,谢谢!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • weixin_41475710 高山听流水 3年前

    代码应该改成这样
    play = input("请输入剪刀(0)石头(1)布(2):")

    player = int(play)

    com = 1

    if (player==0 and com==2) or (player==1 and com==0) or (player==2 and com==1):
    print("你赢了,真厉害")
    elif player==com:
    print("平局,再来一次")
    elif (player==0 and com==1) or (player==1 and com==2) or (player==2 and com==0):
    print("你输了,不要走,决战到天亮")

    注意:python else后面不能接判断条件,if和elif后面才可以加

    点赞 1 评论 复制链接分享
  • weixin_39416561 lyhsdy 3年前

    else改为elif就可以了

    
    play = input("请输入剪刀(0)石头(1)布(2):")
    
    player = int(play)
    
    com = 1
    
    if (player==0 and com==2) or (player==1 and com==0) or (player==2 and com==1):
            print("你赢了,真厉害")
    elif player==com:
            print("平局,再来一次")
    elif (player==0 and com==1) or (player==1 and com==2) or (player==2 and com==0):
            print("你输了,不要走,决战到天亮")
    
    
    点赞 1 评论 复制链接分享
  • RikkaTakanashi RikkaTakanashi 3年前

    第九行else改为elif 或者去掉else后面的条件即可

    点赞 1 评论 复制链接分享
  • qq_39966112 Xiao_冬 3年前
    
    play = input("请输入剪刀(0)石头(1)布(2):")
    player = int(play)
    com = 1
    if (player==0 and com==2) or (player==1 and com==0) or (player==2 and com==1):
        print("你赢了,真厉害")
    elif player==com:
        print("平局,再来一次")
    else:   #else就是除了if和elif之外的任意条件,后面不能再加条件
        print("你输了,不要走,决战到天亮")
    
    

    再附上以前写过的类似程序

    
    #coding=utf-8
    
    #导入random模块,用于产生随机数
    import random
    
    #剪刀、石头、布游戏
    str = "stop"
    player_list = ['剪刀','石头','布']
    print("剪刀(0) 石头(1) 布(2),输入stop退出")
    while True:
        temp = input("请输入对应的数字(或stop退出):")
        if temp == str:
            print("game over!")
            break
        else:
            if temp in ['0','1','2']:
                player = int(temp)
                computer = random.randint(0,2)
                if player-computer==1 or player-computer==-2:
                    print("玩家:{},电脑:{},玩家赢了!!!".format(player_list[player],player_list[computer]))
                elif player == computer:
                    print("玩家:{},电脑:{},平局!!!".format(player_list[player], player_list[computer]))
                else:
                    print("玩家:{},电脑:{},玩家输了!!!".format(player_list[player], player_list[computer]))
            else:
                print("输入错误!")
    
    
    点赞 1 评论 复制链接分享