【问题标题】:Random code not reading? cannot concatenate? [duplicate]乱码不读?不能连接? [复制]
【发布时间】:2016-02-08 04:13:23
【问题描述】:
PlayerAnswer = easygui.enterbox ("What is" +Figure1+ "+" +Figure2+ "?")

这行代码不读,Figure1和Figure2都定义为随机变量,导入random模块。

这是我在代码顶部定义它们的方式:

Figure1 = random.randrange(0,11)
Figure2 = random.randrange(0,11)

我得到的错误是:

TypeError: cannot concatenate 'str' and 'int' objects**

这是上下文中的编码:

for number in range(0,11):
    PlayerAnswer = easygui.enterbox ("What is" +Figure1+ "+" +Figure2+ "?")
    if PlayerAnswer ==(Figure1 + Figure2):
            AdditionAnswers += 1
            easygui.msgbox ("Correct! Your score is "+str(AdditionAnswers))**

【问题讨论】:

  • read的代码是什么意思?
  • 你不能连接字符串和整数。

标签: python random concatenation easygui


【解决方案1】:

您不能将类型为字符串和整数的值相加。为了使其正常工作,您需要使用str 函数将这两个值转换为字符串,如下所示:

PlayerAnswer = easygui.enterbox ("What is" + str(Figure1) + "+" + str(Figure2) + "?")

【讨论】:

  • 非常感谢!现在看起来很明显哈哈,谢谢你的帮助:3
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-13
  • 1970-01-01
相关资源
最近更新 更多