【问题标题】:Int is not callableInt 不可调用
【发布时间】:2015-03-27 03:32:35
【问题描述】:

我已经研究过这个主题,但找不到相关答案,这是我的代码:

#Imports#
import random
from operator import add, sub, mul
import time
from random import choice

#Random Numbers#
beg1 = random.randint(1, 10)
beg2 = random.randint(1, 10)

#Variables + Welcoming message#
correct = 0
questions = 10
print ("Welcome to the Primary School Maths quiz!!")
print ("All you have to do is answer the questions as they come up!")
time.sleep(1)

#Name#
print("Enter your first name")
Fname = input("")
print ("Is this your name?" ,Fname)
awnser = input("")
if awnser == ("yes"):
    print ("Good let's begin!")
    questions()
if input == ("no"):
    print("Enter your first name")
    Fname = input("")
    print ("Good let's begin!")




#Question Code#
def questions():
    for i in range(questions):
    ChoiceOp = random.randint (0,2)
    if ChoiceOp == "0":
        print (("What is " +beg1 ,op ,beg2)) 
        begAns = input("")
        if int(begAns) == beg1*beg2:
            print("That's right -- well done.\n")
            correct = correct +1
        else:
            print("No, I'm afraid the answer is ",begAns)

    if ChoiceOp == "1":
        print (("What is " +beg1 ,op ,beg2)) 
        begAns = input("")
        if int(begAns) == beg1-beg2:
            print("That's right -- well done.\n")
            correct = correct +1
        else:
            print("No, I'm afraid the answer is ",begAns)

    if ChoiceOp == "2":
        print (("What is " +beg1 ,op ,beg2)) 
        begAns = input("")
        if int(begAns) == beg1+beg2:
            print("That's right -- well done.\n")
            correct = correct +1
        else:
            print("No, I'm afraid the answer is ",begAns)
questions()

老实说,我不太确定出了什么问题,我遇到了很多问题小学生的问题任何帮助我提前感谢! :D

【问题讨论】:

  • 那是什么语言?
  • “我已经研究过这个主题,但找不到相关的答案” - 那么你应该提高你的研究技能。只需查看右侧边栏中的相关问题列表。你应该通过谷歌搜索你的问题标题找到了这些。 TL;DR:问题是您的代码中有一个int 变量,您尝试像函数一样调用它;例如 i = 1; i() 导致完全相同的错误。

标签: python int callable


【解决方案1】:

你有一个函数def questions() 和一个变量questions = 10。这在 Python 中不起作用;每个名称只能指一件事:一个变量、一个函数、一个类,但不能每一个都指代,因为这是可能的,例如在 Java 中。

要解决此问题,请将您的变量重命名为,例如 num_questions = 10,或将您的函数重命名为,例如 def ask_question()

还请注意,您在实际定义之前调用了 questions 函数。同样,这适用于其他一些语言,但不适用于 Python。将您的 def quesitons 放在顶部和下面的输入提示中,或者放在另一个函数中,例如def main().

【讨论】:

  • tobias_k 尽管发生了变化,但代码仍然出现相同的错误,但无论如何感谢! :D
  • @khepigamin 那么,你做了什么改变?此外,您的代码中还有一些错误,但我无法为您修复所有这些错误,例如您还在将整数与字符串进行比较,但忘记定义 opcorrect 变量。
猜你喜欢
  • 2015-01-29
  • 2014-03-17
  • 1970-01-01
  • 2022-11-28
  • 2015-12-19
  • 2020-12-24
  • 2017-09-19
  • 2012-04-03
相关资源
最近更新 更多