Python TypeError:一元+;的操作数类型错误:';str';

Python TypeError:一元+;的操作数类型错误:';str';,python,Python,这是我的密码- import random symbols=["+","-","x"] question=0 score=0 choice=0 name=input("What is your name?") while question<10: r1=random.randint(1,10) r2=random.randint(1,10) s1=random.choice(symbols) add=(r1+r2) sub=(r1-r2) t

这是我的密码-

import random
symbols=["+","-","x"]
question=0
score=0
choice=0
name=input("What is your name?")
while question<10:
    r1=random.randint(1,10)
    r2=random.randint(1,10)
    s1=random.choice(symbols)
    add=(r1+r2)
    sub=(r1-r2)
    times=(r1*r2)
    print("What is ",+str(r1),+s1,+str(r2),)
    ask=int(input())
    if s1=="+":
        if ask==add:
            print("Correct")
            score=score+1
        else:
            print("Incorrect")
    if s1=="-":
        if ask==sub:
            print("Correct")
            score=score+1
        else:
            print("Incorrect")
    if s1=="x":
        if ask==sub:
            print("Correct")
            score=score+1
        else:
            print("Incorrect")
print("Your score is: "+score,"out of 10")
随机导入
符号=[“+”、“-”、“x”]
问题=0
分数=0
选择=0
name=输入(“你叫什么名字?”)

当问题时,您需要删除逗号:

print("What is " +str(r1)+s1+str(r2))
print("What is", r1, s1, r2)
最后一行:

print("Your score is: " + score + "out of 10")
但作为一种更具python风格的方式,您可以使用
%
格式

print("What is {0}{1}{2}".format(r1,s1,r2))
或:


您需要删除逗号:

print("What is " +str(r1)+s1+str(r2))
print("What is", r1, s1, r2)
最后一行:

print("Your score is: " + score + "out of 10")
但作为一种更具python风格的方式,您可以使用
%
格式

print("What is {0}{1}{2}".format(r1,s1,r2))
或:


您必须更改行:

print("What is ",+str(r1),+s1,+str(r2),)
为此,有两个选项

  • print(“什么是”+str(r1)+s1+str(r2))
  • print(“什么是%d%s%d”%(r1、s1、r2))
  • 打印(“什么是”、r1、s1、r2)

    • %s
      用于字符串
    • %d
      用于整数
    • 浮动的
      %f.2
  • 您还需要更改最后一行:

    print("Your score is: "+score,"out of 10")
    
    用于:

  • 打印(“您的分数为:“+score+”满分10”)
  • print(“您的分数为%s,共10分”%score)
  • 打印(“您的分数为:”,分数为“满分10”)

  • 您必须更改行:

    print("What is ",+str(r1),+s1,+str(r2),)
    
    为此,有两个选项

  • print(“什么是”+str(r1)+s1+str(r2))
  • print(“什么是%d%s%d”%(r1、s1、r2))
  • 打印(“什么是”、r1、s1、r2)

    • %s
      用于字符串
    • %d
      用于整数
    • 浮动的
      %f.2
  • 您还需要更改最后一行:

    print("Your score is: "+score,"out of 10")
    
    用于:

  • 打印(“您的分数为:“+score+”满分10”)
  • print(“您的分数为%s,共10分”%score)
  • 打印(“您的分数为:”,分数为“满分10”)

  • 不要在打印语句中混用逗号,你会没事的。好的,那么你的问题是什么?打印语句中可能出现重复的逗号,你会没事的。好的,那么你的问题是什么?最后一行可能出现的重复,需要删除逗号并插入加号。@BlackPlanet是的,thansk,我将其添加到回答中。最后一行需要删除逗号并插入加号。@BlackPlanet是的,thansk,我将其添加到回答中