【问题标题】:TypeError: '>' not supported between instances of 'function' and 'int'?TypeError:'function'和'int'的实例之间不支持'>'?
【发布时间】:2020-11-13 20:22:27
【问题描述】:
**Getting error but confused why everything seems to be in place as it should be?**

为什么我会收到错误请解释一切基本上从上到下工作,直到它达到该功能,然后它给了我我试图弄清楚的错误因为我所有的 if elif 和 else声明似乎井井有条并且工作正常

import random
import emoji


def decor(write):
    def wrap():
        print(“Choose a decision!” + “\U0001F604")
        write()
        print("-Testing mode-")
    return wrap()
    
def print_text():
    print("mini project")
decorated = decor(print_text)
print("")



decision = input("please enter decision: " + "")

if decision == ("Right answer"):
    decision = input("I will go to work today!: "+"\U0001F600")
    
    
elif decision ==("Wrong answer"):
    decision = input("Will not go to work today: "+ "\U0001F612")
    
    
else:
    print("Invalid answer try again!")
    

    
if decision == ("Wrong answer"):
     decision = input("But in a bad mood: ")
   
   
elif decision ==("Right answer"):
    decision = input("Your check will come out bigger cause you put in more hours: ")
    print(decision)

    
else:
    print("Invalid answer try again!")
    
    

这是我得到错误的地方

def Decision_maker():
    x = lambda x: x*2 + 1
    if x > 4:
        decision = ("Right answer")
        decision == input("You got a raise!")
        
if decision == ("Wrong answer"):
    decision = input("You got fired: ")
            
        
        

    
Decision_maker()

这是错误

> Traceback (most recent call last):   File
> "/private/var/mobile/Containers/Shared/AppGroup/3EBFD0C8-D5AE-4513-B2E3-6C38570AE9F0/Pythonista3/Documents/site-packages-3/decision maker.py", line 60, in <module>
>     Decision_maker()   File "/private/var/mobile/Containers/Shared/AppGroup/3EBFD0C8-D5AE-4513-B2E3-6C38570AE9F0/Pythonista3/Documents/site-packages-3/decision maker.py", line 49, in Decision_maker
>     if x > 4: TypeError: '>' not supported between instances of 'function' and 'int

    

    

【问题讨论】:

  • x 是这里的一个函数,你没有给它输入任何值。还要避免使用与 lambda 函数相同的变量名,这会让人感到困惑。
  • 老实说只是加入了 Lambda 函数,因为我在练习它们@ZWang
  • 发布回溯消息。我们想要错误和失败行的确切文本。
  • 如果没有 lambda 函数,您仍然没有给 x 任何变量。尝试执行x(1) 之类的操作,错误不会显示。
  • 你去@tdelaney

标签: python function if-statement typeerror


【解决方案1】:

问题出在这里:

x = lambda x: x*2 + 1
    if x > 4:

您传递的是函数,而不是函数的结果。而是尝试:

y = lambda x: x*2 + 1
    if y(value) > 4:

value 是您想要传递给函数的任何内容。通过编写“lambda x:”,您正在创建一个带有参数“x”的匿名函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    • 2020-09-07
    • 2018-09-03
    • 2021-01-15
    • 2020-06-02
    • 2019-08-14
    • 1970-01-01
    相关资源
    最近更新 更多