【问题标题】:Unsupported operand type(s) for +: 'function' and 'int' error+ 不支持的操作数类型:“function”和“int”错误
【发布时间】:2018-11-06 21:03:58
【问题描述】:

我的代码:

def start_input():
    start = int(input("\nAt what number shall we start, master? "))
    return start

def finish_input():
    end = int(input("\nwhen shall i finish, master? "))
    return end

def step_input():
    rise = int(input("\nby what ammount shall your numbers rise, master? "))
    return rise

def universal_step():
    rise = 3
    return rise

def the_counting():
    print("your desired count: ")
    for i in range ( start_input, finish_input +1, step_input): #can be also changed for automated step
        return print(i, finish_input ="  ")

def main():
    start_input()
    finish_input()
    step_input() #This can be changed for the universal_step function for no input if wanted
    the_counting()

main()

input("\n\nPress the enter key to exit.")

所以没有将代码放入以前功能齐全的函数中,现在我得到的只是“+ 的不支持的操作数类型:'function' 和 'int' 错误”,它在 def 计数中功能。我是 python 新手,不知道为什么以及发生了什么。感谢您的帮助:)

【问题讨论】:

  • 你可能想调用那个函数,使用()
  • 您的问题在于您的the_counting() 函数。您尝试添加finish_input1,即使1 是一个int 而finish_input 是一个函数。您不能将函数添加到数字。您可能打算使用 finish_input() 调用您的函数
  • 我立即想到的问题是 i in range ( start_input, finish_input +1 基本上,您正在尝试将整数添加到函数中,这不是有效的操作。函数是一种做某事的方法。整数就是整数。
  • 你也是returnprint(),做一个或另一个,或者打印然后返回

标签: python function for-loop


【解决方案1】:

您在range 中使用的所有东西都是函数,而不是变量;你必须调用(添加调用括号)他们来获得他们的价值,改变:

for i in range ( start_input, finish_input +1, universal_step):

到(PEP8 间距):

for i in range(start_input(), finish_input() + 1, universal_step()):

【讨论】:

    猜你喜欢
    • 2012-10-07
    • 1970-01-01
    • 2018-09-01
    • 2013-10-04
    • 2021-06-16
    • 2014-06-16
    • 2014-03-16
    • 2020-08-26
    • 1970-01-01
    相关资源
    最近更新 更多