1)为什么有 "invalid syntax" (代码中突出显示的行)? 2)错误解决后,为什么只有python读到if语句?
1)Why is there an "invalid syntax" (line highlighted in code)? 2) After the error is solved, why is only the if statement read by python?
我正在编写一个 2 人骰子游戏,其中随机掷出两个 6 面骰子。如果骰子的总和为偶数,则投掷的两个骰子的总和为 +10。如果骰子的总和为奇数,则掷出的骰子总数为 -5。如果用户掷双骰,他们将掷另一个骰子,他们的分数是所有 3 个骰子的总和。
有 5 轮,显示的代码是玩家 1 的第一轮。
1) 为什么突然出现 "invalid syntax"(倒数第二行,在代码中突出显示)?
2) 为什么只读if语句? (其他两个 elif 语句被忽略)
即使掷出双倍或偶数,游戏仍会从两个骰子的总和中减去 5,无论结果如何。
提前致谢。
下面是我的代码:
import time
import random
print("Rolling dice for Player 1...")
time.sleep(1)
P1_dice1A = (random.randint(1, 6)) #1st die
print("Dice 1 =",str(P1_dice1A)) #prints 1st die
time.sleep(1)
P1_dice1B = (random.randint(1, 6)) #2nd die
print("Dice 2 =",str(P1_dice1B)) #prints 2nd die
P1_dicetotal1 = P1_dice1A + P1_dice1B #adds both die
print("You rolled",str(P1_dicetotal1)) #prints result of line above
P1_score = 0 #total score for all 5 rounds, starts at 0
if P1_dicetotal1 == 1 or 3 or 5 or 7 or 9 or 11:
print("Oh no! You rolled an odd number, so -5 points from your score :(.")
P1_score_r1 = P1_dicetotal1 - 5 #subtracts 5 from total score and score this round
print("Player 1's score this round =",str(P1_score_r1)) #prints score this round
P1_score == P1_score_r1 #total score is same as score this round because this is round 1 out of 5 for player 1
print(P1_score) #prints total score
if P1_score_r1 < 0:
print("Unlucky. Your score reached below 0. Game Over.")
print("Thank you for playing and I hope you enjoyed playing.")
import sys
sys.exit()
elif P1_dice1A == P1_dice1B: #if dice are the same
print("You rolled a double, so you get to roll another dice...")
time.sleep(1)
P1_dice1C = (random.randint(1, 6)) #3rd die is rolled
P1_score_r1 = P1_dicetotal1 + P1_dice1C #adds die 1, 2 and 3 to toal for this round and whole game
print("Player 1's score this round =",str(P1_score_r1))
P1_score == P1_score_r1
print(P1_score)
elif P1_dicetotal1 == 2 or 4 or 6 or 8 or 10 or 12:
print("Your total was an even number, so +10 points to your total.")
P1_score_r1 = P1_dicetotal1 + 10 #adds 10 to total score and score this round
print("Player 1' score this round =",str(P1_score_r1)
P1_score == P1_score_r1 #ERROR LINE - "P1_score" is highlighted red
print(P1_score) #prints total score after every round
您需要关闭倒数第三行的 print
语句:
print("Player 1' score this round =",str(P1_score_r1)
应该是:
print("Player 1' score this round =",str(P1_score_r1))
另外,您的 if
s 陈述是错误的,您需要将 P1_dicetotal1 == 1
与所有其他值一起使用,这就是为什么它总是正确的。
我正在编写一个 2 人骰子游戏,其中随机掷出两个 6 面骰子。如果骰子的总和为偶数,则投掷的两个骰子的总和为 +10。如果骰子的总和为奇数,则掷出的骰子总数为 -5。如果用户掷双骰,他们将掷另一个骰子,他们的分数是所有 3 个骰子的总和。 有 5 轮,显示的代码是玩家 1 的第一轮。
1) 为什么突然出现 "invalid syntax"(倒数第二行,在代码中突出显示)?
2) 为什么只读if语句? (其他两个 elif 语句被忽略) 即使掷出双倍或偶数,游戏仍会从两个骰子的总和中减去 5,无论结果如何。
提前致谢。 下面是我的代码:
import time
import random
print("Rolling dice for Player 1...")
time.sleep(1)
P1_dice1A = (random.randint(1, 6)) #1st die
print("Dice 1 =",str(P1_dice1A)) #prints 1st die
time.sleep(1)
P1_dice1B = (random.randint(1, 6)) #2nd die
print("Dice 2 =",str(P1_dice1B)) #prints 2nd die
P1_dicetotal1 = P1_dice1A + P1_dice1B #adds both die
print("You rolled",str(P1_dicetotal1)) #prints result of line above
P1_score = 0 #total score for all 5 rounds, starts at 0
if P1_dicetotal1 == 1 or 3 or 5 or 7 or 9 or 11:
print("Oh no! You rolled an odd number, so -5 points from your score :(.")
P1_score_r1 = P1_dicetotal1 - 5 #subtracts 5 from total score and score this round
print("Player 1's score this round =",str(P1_score_r1)) #prints score this round
P1_score == P1_score_r1 #total score is same as score this round because this is round 1 out of 5 for player 1
print(P1_score) #prints total score
if P1_score_r1 < 0:
print("Unlucky. Your score reached below 0. Game Over.")
print("Thank you for playing and I hope you enjoyed playing.")
import sys
sys.exit()
elif P1_dice1A == P1_dice1B: #if dice are the same
print("You rolled a double, so you get to roll another dice...")
time.sleep(1)
P1_dice1C = (random.randint(1, 6)) #3rd die is rolled
P1_score_r1 = P1_dicetotal1 + P1_dice1C #adds die 1, 2 and 3 to toal for this round and whole game
print("Player 1's score this round =",str(P1_score_r1))
P1_score == P1_score_r1
print(P1_score)
elif P1_dicetotal1 == 2 or 4 or 6 or 8 or 10 or 12:
print("Your total was an even number, so +10 points to your total.")
P1_score_r1 = P1_dicetotal1 + 10 #adds 10 to total score and score this round
print("Player 1' score this round =",str(P1_score_r1)
P1_score == P1_score_r1 #ERROR LINE - "P1_score" is highlighted red
print(P1_score) #prints total score after every round
您需要关闭倒数第三行的 print
语句:
print("Player 1' score this round =",str(P1_score_r1)
应该是:
print("Player 1' score this round =",str(P1_score_r1))
另外,您的 if
s 陈述是错误的,您需要将 P1_dicetotal1 == 1
与所有其他值一起使用,这就是为什么它总是正确的。