1) 为什么会有一个;无效语法";(代码中突出显示的行)?2) 解决错误后,为什么python只读取if语句?
我正在写一个2人骰子游戏,其中两个6面骰子随机滚动。如果骰子总数为偶数,则掷出的两个骰子之和+10。如果骰子总数为奇数,则掷骰子总数的-5。如果用户掷双倍骰子,他们将掷另一个骰子,其分数为所有3个骰子的总和。 共有5轮,显示的代码是玩家1的第一轮 1) 为什么突然出现“无效语法”(最后第二行,代码中突出显示) 2) 为什么只读取if语句?(忽略其他两条elif语句) 即使掷双打或偶数,游戏仍然会从两个骰子的总和中减去5,无论结果如何 提前谢谢。 下面是我的代码:1) 为什么会有一个;无效语法";(代码中突出显示的行)?2) 解决错误后,为什么python只读取if语句?,python,python-3.x,python-3.3,Python,Python 3.x,Python 3.3,我正在写一个2人骰子游戏,其中两个6面骰子随机滚动。如果骰子总数为偶数,则掷出的两个骰子之和+10。如果骰子总数为奇数,则掷骰子总数的-5。如果用户掷双倍骰子,他们将掷另一个骰子,其分数为所有3个骰子的总和。 共有5轮,显示的代码是玩家1的第一轮 1) 为什么突然出现“无效语法”(最后第二行,代码中突出显示) 2) 为什么只读取if语句?(忽略其他两条elif语句) 即使掷双打或偶数,游戏仍然会从两个骰子的总和中减去5,无论结果如何 提前谢谢。 下面是我的代码: import time impo
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滚动骰子…”)
时间。睡眠(1)
P1_dice1A=(random.randint(1,6))#第一个骰子
打印(“骰子1=,str(P1_骰子1a))#打印第一个骰子
时间。睡眠(1)
P1_dice1B=(random.randint(1,6))#第二个骰子
打印(“骰子2=,str(P1_骰子1b))#打印第二个骰子
P1_dicetotal1=P1_dice1A+P1_dice1B#将两个骰子相加
打印(“您滚动”,str(P1_dicetotal1))#打印上面一行的结果
P1_分数=0#所有5轮的总分从0开始
如果P1_dicetotal1==1或3或5或7或9或11:
打印(“哦,不!你滚了一个奇数,所以你的分数是-5分:(。”)
P1_得分_r1=P1_dicetotal1-5#从总分中减去5并在本轮得分
打印(“玩家1本轮得分=”,str(P1_得分_r1))#打印本轮得分
P1_分数==P1_分数_r1#总分与本轮分数相同,因为这是球员1的第1轮,共5轮
打印(P1#U分数)#打印总分
如果P1_得分_r1<0:
打印(“不走运。你的分数低于0。游戏结束。”)
打印(“感谢您的演奏,我希望您喜欢演奏。”)
导入系统
sys.exit()
elif P1_dice1A==P1_dice1B:#如果骰子相同
打印(“你掷了一个双骰子,所以你可以掷另一个骰子…”)
时间。睡眠(1)
P1_dice1C=(random.randint(1,6))#第三个骰子被滚动
P1_score_r1=P1_dicetotal1+P1_dice1C#为本轮和整场比赛的总比分加上骰子1、2和3
打印(“玩家1本轮得分=”,str(P1_得分_r1))
P1_分数==P1_分数_r1
打印(P1_分数)
elif P1_dicetotal1==2或4或6或8或10或12:
打印(“你的总数是偶数,所以你的总数加10分。”)
P1_得分_r1=P1_dicetotal1+10#总得分加10,本轮得分
打印(“玩家1”本轮得分=”,str(P1\U得分\U r1)
P1_分数==P1_分数_r1#错误行-“P1_分数”高亮显示为红色
打印(P1#U分数)#每轮后打印总分
您需要关闭最后第三行的打印
语句:
print(“玩家1”本轮得分=”,str(P1_得分_r1)
应该是:
print(“玩家1”本轮得分=“,str(P1_得分_r1))
另外,如果你的
语句错误,你需要将P1\u dicetotal1==1
与所有其他值一起使用,这就是为什么它总是正确的原因。关于1)看这一行上面的一行。底部的第三行缺少一个右括号,关于2)在if
条件下,您必须为每个或重复P1_dicetotal1==
。它不能按您编写的方式工作。或者,您可以在中使用。如果您只查看奇数或偶数,您应该这样做-P1_dicetotal1%2==1
P1_dicetotal1%2==0
谢谢非常感谢