简体   繁体   English

* 不支持的操作数类型:'NoneType' 和 'NoneType'

[英]unsupported operand type(s) for *: 'NoneType' and 'NoneType'

def main():
dollars = dollars_to_float(input("How much was the meal? "))
percent = percent_to_float(input("What percentage would you like to tip? "))
tip = dollars * percent
print(f"Leave ${tip:.2f}")
def dollars_to_float(d):
    d = d.replace('$', '')
    d = float(d)
    print(d)
def percent_to_float(p):
    p = p.replace('%', '')
    p = float(p)
    p = p/100
    print(p)

main()

tried to convert "dollars" and "percent" but it didn't work, I searched but most of the things didn't work and i didn't understand some because I'm new to python试图转换“美元”和“百分比”但它没有用,我搜索了但大部分东西都没有用而且我不明白一些因为我是 python 的新手

You aren't returning anything from your functions.你没有从你的函数中返回任何东西。 If you want a value returned, then you must do so.如果你想返回一个值,那么你必须这样做。 Use return p instead of print(p) .使用return p而不是print(p) Utility functions should not PRINT their results.效用函数不应打印其结果。 They should RETURN their results and let the caller decide what to do with it.他们应该返回他们的结果并让调用者决定如何处理它。

def dollars_to_float(d):
    d = d.replace('$', '')
    d = float(d)
    return d
def percent_to_float(p):
    p = p.replace('%', '')
    p = float(p)
    p = p/100
    return p

functions are something that return a value: it can be anything like int or string... if you dont return anything at end of your caculation in function, it return None that means null or nothing and you cant use None in arithmatic statement, so return d or p, instead of print and use it in your main function:函数是返回一个值的东西:它可以是任何东西,比如 int 或 string...如果你在 function 的计算结束时没有返回任何东西,它返回 None 意味着 null 或什么都没有,你不能在算术语句中使用 None,所以返回 d 或 p,而不是打印并在您的主 function 中使用它:

def dollars_to_float(d):
    d = d.replace('$', '')
    d = float(d)
    return d

def percent_to_float(p):
    p = p.replace('%', '')
    p = float(p)
    p = p/100
    return p

def main():
    dollars = dollars_to_float(input("How much was the meal? "))
    print(dollars)
    percent = percent_to_float(input("What percentage would you like to tip? "))
    print(percent)
    tip = dollars * percent
    print(f"Leave ${tip:.2f}")

main()

any function send it's last value to others by return and others can use it for print or other calculation任何 function 通过返回将其最后的值发送给其他人,其他人可以将其用于打印或其他计算

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:/:'NoneType'和'NoneType'不受支持的操作数类型 - TypeError: unsupported operand type(s) for /: 'NoneType' and 'NoneType' 类型错误:+ 不支持的操作数类型:“NoneType”和“NoneType” - TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType *:'NoneType'和'NoneType'(Python)不受支持的操作数类型 - Unsupported operand type(s) for *: 'NoneType' and 'NoneType' (Python) 类型错误:+ 不支持的操作数类型:“NoneType”和“NoneType” - TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType' TypeError:-:“ NoneType”和“ NoneType”的不受支持的操作数类型 - TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType' Pytorch 梯度错误:n.netype 不支持 + 的操作数类型:'NoneType' 和 'NoneType' - Pytorch gradient error: nonetype unsupported operand type(s) for +: 'NoneType' and 'NoneType' + =不支持的操作数类型:“ NoneType”和“ list” - unsupported operand type(s) for +=: 'NoneType' and 'list' * 不支持的操作数类型:'int' 和 'NoneType' - Unsupported operand type(s) for *: 'int' and 'NoneType' TypeError: 不支持的操作数类型 -: 'float' 和 'NoneType' - TypeError: unsupported operand type(s) for -: 'float' and 'NoneType' TypeError:+不支持的操作数类型:“ NoneType”和“ str” - TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM