Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python int()可以';t使用显式基转换非字符串_Python - Fatal编程技术网

Python int()可以';t使用显式基转换非字符串

Python int()可以';t使用显式基转换非字符串,python,Python,这是我的代码: 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?

这是我的代码:

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")
questions = easygui.enterbox(msg = "enter your desired amount of questions")

for a in range(int(questions)):
  rn1 = randint(int(Minimum), int(Maximum))
  rn2 = randint(int(Minimum), int(Maximum))
  answer = easygui.enterbox("%s %s %s =?" %(rn1, operator, rn2))
  realanswer = operator (int(rn1,rn2))
  if answer == realanswer:
   print "Correct"
  else:
   print 'Incorrect, the answer was' ,realanswer
当我尝试运行它时,所有的enterbox都显示良好,当它查看第13行时,它产生了以下错误:

int()无法转换具有显式基的非字符串

我尝试在不使用
int()
的情况下运行代码,然后它给出:

“str”对象不可调用


运算符
变量包含一个字符串。您必须使用该字符串来确定要执行的实际操作

诸如此类:

if operator == "+":
      realanswer = rn1 + rn2
elif operator == "-":
      realanswer = rn1 - rn2
elif operator == "/":
      realanswer = rn1 / rn2
elif operator == "*":
      realanswer = rn1 * rn2
else
      raise Exception('Bad operator {}'.format(operator))
或更好地使用:


当然,在实际的应用程序中,您必须以某种方式处理“无效”的用户输入。例如,使用适当的异常处理。但这是另一个故事…

首先:您的
操作符是字符串,而不是函数。您不能调用
'/'(2,3)
,因此如果
运算符=='/'
,您仍然不能调用
运算符(2,3)

第二:
int(rn1),int(rn2)
是如何将两个不同的数字转换为整数,而不是
int(rn1,rn2)

第三:从
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")
questions = int(easygui.enterbox(msg="enter your desired amount of questions"))

# Select a function associated with the chosen operator
operators = {
    '*': lambda a,b: a*b,
    '/': lambda a,b: a/b,
    '+': lambda a,b: a+b,
    '-': lambda a,b: a-b,
}
operator_fn = operators.get(operator)
if operator_fn is None:
    raise Exception('Unknown operator %r' % operator)

for a in range(questions):
    rn1 = randint(minimum, maximum))
    rn2 = randint(minimum, maximum))
    answer = int(easygui.enterbox("%s %s %s = ?" % (rn1, operator, rn2)))
    realanswer = operator_fn(rn1,rn2)
    if answer == realanswer:
        print "Correct"
    else:
        print 'Incorrect, the answer was', realanswer

操作符
只是一个字符串,您仍然需要编写代码,使其具有某种意义。你可以这样做:

if operator in ('+', 'add'):
    realanswer = rn1 + rn2
elif operator in ('-', 'subtract'):
    realanswer = rn1 - rn2
else:
    print operator, "is not valid"

str不可调用
是因为运算符是字符串。简言之,这是修复此错误后出现的下一个无关错误。:)也就是说,如果您得到的
'str'对象是不可调用的
,您已经修复了
int()无法转换非字符串
错误,因此您已经知道如何修复它,并且不需要在这里问问题。:)顺便说一句,下次你问问题的时候,试着把它分成一件事。例如,包括不必要的依赖项,如
easygui
库,会使人们更难重现问题——如果你真的只是问
int()不能转换非字符串的问题,你可以只用一行就可以做到,比如:“为什么
int(3,5)
引发此异常?”(如果您打印了
rn1
rn2
以获取样本值)。有关询问包括代码在内的高质量重点问题的更多类似提示,请参阅。@SylvainLeroux,我希望您能原谅我抄袭dict方法。(尽管我可以指出我自己的一些代码也做了同样的事情,如果我15年前的大学项目没有从SourceForge中删减的话)。没问题。只要有可能,我就使用dict方法,因为它有我喜欢的“声明式风格”——有助于编写干净的错误处理代码——并且通常易于维护。
if operator in ('+', 'add'):
    realanswer = rn1 + rn2
elif operator in ('-', 'subtract'):
    realanswer = rn1 - rn2
else:
    print operator, "is not valid"