努力的小bai
2018-10-12 10:59
采纳率: 100%
浏览 815
已采纳

简单的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条回答 默认 最新

  • 高山听流水 2018-10-12 11:48
    已采纳

    代码应该改成这样
    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后面才可以加

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • Xiao_冬 2018-10-12 13:00
    
    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("输入错误!")
    
    
    评论
    解决 无用
    打赏 举报
  • RikkaTakanashi 2018-10-12 13:32

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

    评论
    解决 无用
    打赏 举报
  • lyhsdy 2018-10-13 01:25

    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("你输了,不要走,决战到天亮")
    
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题