Python 不支持的操作数类型(int)和#x2B;(var)错误
作为一个错误,但我不明白var+int在哪里。我需要帮助。Python 不支持的操作数类型(int)和#x2B;(var)错误,python,Python,作为一个错误,但我不明白var+int在哪里。我需要帮助。 顺便说一句,我不太擅长这个陷阱。请删除下面为P1R1和P2R2分配元组的行 P1Roundx=int (0) P2Roundx=int(0) P1Scorex=int(0) P2Scorex=int(0) P1Total=int(0) P2Total=int(0) number=int(0) P1R1=int(0) P2R1=int(0) P1R2=int(0) P2R2=int(0) import time Username=in
顺便说一句,我不太擅长这个陷阱。请删除下面为P1R1和P2R2分配元组的行
P1Roundx=int (0)
P2Roundx=int(0)
P1Scorex=int(0)
P2Scorex=int(0)
P1Total=int(0)
P2Total=int(0)
number=int(0)
P1R1=int(0)
P2R1=int(0)
P1R2=int(0)
P2R2=int(0)
import time
Username=input("what is player 1’s username")
Username=str(Username)
if Username == ("1"):
print ("hello",Username,)
elif Username == ("2"):
print ("welcome",Username,)
else:
print("LEAVE!")
exit()
# This module determines if player 1 is authenticated or if they are not. If they are not they are forced to exit the programme
Username2=input("what is player 2's username")
Username2=str(Username2)
if Username2 == ("2"):
print ("hello,",Username2,)
elif Username2 == ("1"):
print ("welcome",Username2,)
else:
print("LEAVE!")
exit()
# This module determines if player 2 is authenticated or not and exits the programme if they are not
import random
P1R1= random.randint(1,6)
P1R1=("Your first roll is",P1R1)
P1R2=random.randint(1,6)
P1R2=("Your second roll is",P1R2)
print("Your total is",P1R1+P1R2)
P1total=P1R1+P1R2
if P1Total % 2 == 0:
P1Total=(P1Total)+P1R1
else:
P1Total=(P1Total)-5+P1R1+P1R2
print("Player 1's total score is",P1Total)
import random
P2R1= random.randint(1,6)
P2R1=("Your first roll is",P2R1)
P2R2=random.randint(1,6)
P2R2=("Your second roll is",P2R2)
print("Your total is",P2R1+P2R2)
P2total=P2R1+P2R2
if P2Total % 2 == 0:
P2Total=(P2Total)+ P2R1+P2R2
else:
P2Total=(P2Total)-5+P2R1
print("Player 2's total score is",P2Total)
time.sleep(6)
当您的代码执行此操作时
P1R1=("Your first roll is",P1R1)
P2R2=("Your second roll is",P2R2)
P1R1
是一个介于1和6之间的数字;让我们想象它是4。但代码确实如此
P1R1= random.randint(1,6)
看来您的目的是将其打印为消息。但它实际做的是将P1R1
的值更改为tuple(“您的第一卷是”,4)
。然后您的代码继续运行,就好像P1R1
仍然有值4
,因为它在上面做加法
如果您查看此行的输出:
P1R1=("Your first roll is",P1R1)
你会发现这是真的
print("Your total is",P1R1+P1R2)
当你清楚地知道你在期待
Your total is ('Your first roll is', 4, 'Your second roll is', 1)
您应该很容易理解代码在那之后出错的原因。回溯应该告诉您
int
+tuple
错误的来源。看起来P1R1
是一个tuple。但这是不可能确定的,因为您还没有包含定义它的代码。如果我们没有代码,就无法重现问题。请阅读并编辑你的问题,上面写着第42行,也就是这一行。如果我给出完整的答案会有帮助吗code@AdamBrass我们更喜欢a而不是完整的代码。(但老实说,我认为您能够自己调试它。:)当我运行此程序时,我得到一个UnboundLocalError,因为您问题中的代码与名称P1Total
/P1Total
不一致。所以给你错误信息的代码不是你问题中的代码。我怎么说你是上帝
Your total is 5