python骰子游戏中的语法错误

时间:2015-09-15 17:21:06

标签: python

我遇到语法错误,无法找出有问题的人可以帮忙

    import random

play=input("would you like to play Y/N")

if play==Y:
    dice_roll
if play==N:
    print("try again")
    play=input("would you like to play Y/N")

#declare dice as global and rolls dice 
def dice_roll():
    global p1_dice_1
    global p1_dice_2
    global p1_dice_3
    global p2_dice_1
    global p2_dice_2
    global p2_dice_3
    p1_dice_1=random.randint(1,6)
    p1_dice_2=random.randint(1,6)
    p1_dice_3=random.randint(1,6)
    p2_dice_1=random.randint(1,6)
    p2_dice_2=random.randint(1,6)
    p2_dice_3=random.randint(1,6)
    print_test()
    return

def print_test():
    print("player 1 rolled",p1_dice_1,",",p1_dice_2,"and",p1_dice_3)
    print("palyer 2 rolled",p2_dice_1,",",p2_dice_2,"and",p2_dice_3)
    score_rule()
    return

如果这可以变得不那么复杂,我不会介意帮助

 score_rule():
        if p1_dice_1==p1_dice_2==p1_dice_3:
            print("player 1 scored",p1_dice_1+p1_dice_2+p1_dice_3)
        elif p1_dice_1==p1_dice_2=!p1_dice_3:
            print("player 1 scored",p1_dice_1+p1_dice_2-p1_dice_3)
        elif p1_dice_1==p1_dice_3=!p1_dice_2:
            print("player 1 scored",p1_dice_1+p1_dice_3-p1_dice_2)
        elif p1_dice_2==p1_dice_3=!p1_dice_1:
            print("player 1 scored",p1_dice_2+p1_dice_3-p1_dice_1)

    if p2_dice_1==p2_dice_2==p2_dice_3:
        print("player 2 scored",p2_dice_1+p2_dice_2+p2_dice_3)
    elif p2_dice_1==p2_dice_2=!p2_dice_3:
        print("player 2 scored",p2_dice_1+p2_dice_2-p2_dice_3)
    elif p2_dice_1==p2_dice_3=!p2_dice_2:
        print("player 2 scored",p2_dice_1+p2_dice_3-p2_dice_2)
    elif p2_dice_2==p2_dice_3=!p2_dice_1:
        print("player 2 scored",p2_dice_2+p2_dice_3-p2_dice_1)
    return

如果有人知道该怎么做帮助,那么我不得不在问题中加入更多的话,因为代码太多了(显然)

2 个答案:

答案 0 :(得分:3)

  • import random缩进不正确。
  • score_rule的主体不一致缩进。
  • score_rule():应为def score_rule():
  • “=”!不是合法的经营者。使用“!=”。
  • YN未定义。使用字符串文字。
  • dice_roll需要在之前定义才能引用它。移动主代码,使其出现在函数定义之后。
  • 函数调用需要括号。将dice_roll更改为dice_roll()

import random

#declare dice as global and rolls dice 
def dice_roll():
    global p1_dice_1
    global p1_dice_2
    global p1_dice_3
    global p2_dice_1
    global p2_dice_2
    global p2_dice_3
    p1_dice_1=random.randint(1,6)
    p1_dice_2=random.randint(1,6)
    p1_dice_3=random.randint(1,6)
    p2_dice_1=random.randint(1,6)
    p2_dice_2=random.randint(1,6)
    p2_dice_3=random.randint(1,6)
    print_test()
    return

def print_test():
    print("player 1 rolled",p1_dice_1,",",p1_dice_2,"and",p1_dice_3)
    print("palyer 2 rolled",p2_dice_1,",",p2_dice_2,"and",p2_dice_3)
    score_rule()
    return

def score_rule():
    if p1_dice_1==p1_dice_2==p1_dice_3:
        print("player 1 scored",p1_dice_1+p1_dice_2+p1_dice_3)
    elif p1_dice_1==p1_dice_2!=p1_dice_3:
        print("player 1 scored",p1_dice_1+p1_dice_2-p1_dice_3)
    elif p1_dice_1==p1_dice_3!=p1_dice_2:
        print("player 1 scored",p1_dice_1+p1_dice_3-p1_dice_2)
    elif p1_dice_2==p1_dice_3!=p1_dice_1:
        print("player 1 scored",p1_dice_2+p1_dice_3-p1_dice_1)

    if p2_dice_1==p2_dice_2==p2_dice_3:
        print("player 2 scored",p2_dice_1+p2_dice_2+p2_dice_3)
    elif p2_dice_1==p2_dice_2!=p2_dice_3:
        print("player 2 scored",p2_dice_1+p2_dice_2-p2_dice_3)
    elif p2_dice_1==p2_dice_3!=p2_dice_2:
        print("player 2 scored",p2_dice_1+p2_dice_3-p2_dice_2)
    elif p2_dice_2==p2_dice_3!=p2_dice_1:
        print("player 2 scored",p2_dice_2+p2_dice_3-p2_dice_1)
    return

play=input("would you like to play Y/N")

if play=='Y':
    dice_roll()
if play=='N':
    print("try again")
    play=input("would you like to play Y/N")

结果:

would you like to play Y/NY
player 1 rolled 6 , 4 and 5
palyer 2 rolled 3 , 4 and 6

如果没有两个骰子相等,你可能还需要额外的else条款。

答案 1 :(得分:0)

查看错误消息

很有用

但如果那是您的代码,请尝试

if play=='Y':

类似于'N'陈述。

编辑: 答案2也有效,dice_roll是一个函数,dice_roll()。

再次编辑:有人回复了一个非常全面的答案,请参考他的。