[英]Unsupported operand types (int)+(var) error
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)
作為一個錯誤,但我不明白 var + int 在哪里。 我需要幫助。 順便說一句,我不太擅長這個 SNAKEY TING
當您的代碼執行此操作時
P1R1= random.randint(1,6)
P1R1
是 1 到 6 之間的數字; 讓我們假設它是 4。但是代碼確實如此
P1R1=("Your first roll is",P1R1)
看來您的意圖是將其作為消息打印出來。 但它實際上所做的是將P1R1
的值更改為元組("Your first roll is",4)
。 然后您的代碼繼續執行,好像P1R1
仍然具有值4
因為它對它進行了加法。
如果您查看此行的輸出:
print("Your total is",P1R1+P1R2)
你會看到它是
Your total is ('Your first roll is', 4, 'Your second roll is', 1)
當很明顯你期待
Your total is 5
你應該很容易明白為什么代碼在那之后出錯了。
嗨刪除以下將元組分配給 P1R1 和 P2R2 的行
P1R1=("Your first roll is",P1R1)
P2R2=("Your second roll is",P2R2)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.