【问题标题】: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?1)为什么会出现“无效语法”(代码中突出显示的行)? 2)错误解决后,为什么python只读取if语句?
【发布时间】:2020-04-22 08:19:53
【问题描述】:

我正在编写一个 2 人骰子游戏,其中随机掷出两个 6 面骰子。如果骰子的总和是偶数,则掷出的两个骰子的总和为+10。如果骰子的总和是奇数,则掷出的骰子总数为-5。如果用户掷双倍,他们掷另一个骰子,他们的分数是所有 3 个骰子的总和。 有 5 轮,显示的代码是玩家 1 的第 1 轮。

1) 为什么突然出现“无效语法”(代码中突出显示的倒数第二行)?

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

【问题讨论】:

  • 关于 1) 在这条线上看一个。
  • 倒数第三行缺少右括号
  • 关于 2) 在if 条件下,您必须为每个or 重复P1_dicetotal1 ==。它不像你写的那样工作。或者,您可以使用in
  • 鉴于您只查看数字是奇数还是偶数,您应该这样做 - P1_dicetotal1 % 2 == 1 P1_dicetotal1 % 2 == 0

标签: python python-3.x python-3.3


【解决方案1】:

需要关闭倒数第三行的print语句:

print("Player 1' score this round =",str(P1_score_r1)

应该是:

print("Player 1' score this round =",str(P1_score_r1))

另外你的ifs 语句是错误的,你需要将P1_dicetotal1 == 1 与所有其他值一起使用,这就是为什么它总是正确的。

【讨论】:

  • 非常感谢
猜你喜欢
  • 1970-01-01
  • 2020-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多