繁体   English   中英

python骰子游戏中的语法错误

[英]syntax error in python dice game

我有一个语法错误,无法弄清楚出了什么问题有人可以帮忙

    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

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

  • import random缩进不正确。
  • score_rule的主体缩进不一致。
  • score_rule():应该是def score_rule():
  • “=!” 不是合法经营者。 使用“!=”。
  • YN没有定义。 使用字符串文字。
  • dice_roll需要引用之前定义。 移动您的主代码,使其出现在您的函数定义之后。
  • 函数调用需要括号。 更改dice_rolldice_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子句,当没有两个骰子相等时,这些子句会得分。

查看错误消息会很有用

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

if play=='Y':

'N' 语句类似。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM