执行以下代码
import easygui
from random import randint
Minimum = easygui.enterbox(msg = "Choose your minimum number")
Maximum = easygui.enterbox(msg = "Choose your maximum number")
operator = easygui.enterbox( msg="which operator would you like to use? X,/,+ or - ?",title="operator")
rn1 = randint(Minimum,Maximum)
rn2 = randint(Minimum, Maximum)
print "%s %s %s =?" %(rn1, operator, rn2)
给我这个错误
TypeError: cannot concatenate 'str' and 'int' objects
我知道这是字符串和整数之间的冲突,但我该如何修复呢?
答案 0 :(得分:2)
enterbox
返回一个字符串。在存储之前将值转换为整数。
import easygui
from random import randint
Minimum = int(easygui.enterbox(msg = "Choose your minimum number"))
Maximum = int(easygui.enterbox(msg = "Choose your maximum number"))
operator = easygui.enterbox( msg="which operator would you like to use? X,/,+ or - ?",title="operator")
rn1 = randint(Minimum,Maximum)
rn2 = randint(Minimum, Maximum)
print "%s %s %s =?" %(rn1, operator, rn2)
答案 1 :(得分:2)
最小值和最大值是字符串,而不是整数,因此您无法将它们传递给randint。
虽然你应该注意例外,如果他们输入的东西不是int,你可以randint(int(Minimum), int(Maximum))
,只要他们输入的数字很好,你就可以全部设定。