Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.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 TypeError:float()参数必须是字符串或数字_Python - Fatal编程技术网

Python TypeError:float()参数必须是字符串或数字

Python TypeError:float()参数必须是字符串或数字,python,Python,我在高中读计算机科学11年级,我刚刚开始学习Python。我应该创建一个名为computepay的函数,它将询问用户他们的姓名、工资和他们那周的工作时间,并自动计算总数,包括任何加班时间,并且在输入错误时不会出错。我为所有输入创建了不同的函数,但当我将其全部插入我的computepay函数时,它告诉我: TypeError:float()参数必须是字符串或数字 这些函数都没有返回任何内容 name = getname() wage = getwage() hours = geth

我在高中读计算机科学11年级,我刚刚开始学习Python。我应该创建一个名为
computepay
的函数,它将询问用户他们的姓名、工资和他们那周的工作时间,并自动计算总数,包括任何加班时间,并且在输入错误时不会出错。我为所有输入创建了不同的函数,但当我将其全部插入我的
computepay
函数时,它告诉我:

TypeError:float()参数必须是字符串或数字


这些函数都没有返回任何内容

name = getname()    
wage = getwage()    
hours = gethours()
因此,它们最终都是
None

试试这个

def getname():
    return input("What's your name?")  # input of name

def getwage():
    wage = input("Hello there! How much money do you make per hour?")  # input
    return float(wage)

def gethours():
    hours = input("Thanks, how many hours have you worked this week?")
    return float(hours)

错误消息告诉您的是,您正在调用
float
,并给它一个参数(即输入),该参数既不是数字,也不是字符串

你能追踪到这是在哪里发生的吗


提示:任何不返回任何内容(带有
return
关键字)的Python函数都会隐式返回
None
(既不是字符串也不是数字)。

看到发生了什么会有点困惑,因为代码将其格式化,但错误消息会提示您要查找什么-'TypeError:float()参数必须是字符串或数字'。在您的一个浮动调用(例如浮动(工资))中,您传递的内容(例如工资)无法转换为数字。我建议您打印出每个变量以查看其中的内容-因此,在
wage=getwage()之后添加另一行
print(wage)
def getname():
    return input("What's your name?")  # input of name

def getwage():
    wage = input("Hello there! How much money do you make per hour?")  # input
    return float(wage)

def gethours():
    hours = input("Thanks, how many hours have you worked this week?")
    return float(hours)