我遇到语法错误,无法找出有问题的人可以帮忙
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
如果有人知道该怎么做帮助,那么我不得不在问题中加入更多的话,因为代码太多了(显然)
答案 0 :(得分:3)
import random
缩进不正确。score_rule
的主体不一致缩进。score_rule():
应为def score_rule():
Y
和N
未定义。使用字符串文字。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()。
再次编辑:有人回复了一个非常全面的答案,请参考他的。