2014-10-11 74 views
-1

我在11年级的计算机科学高中,我刚刚开始用Python。我应该做一个叫computepay的函数,这个函数会询问用户他们的姓名,他们的工资和他们在这个星期的小时数,并自动计算总数,包括任何加班时间,并且当包含错误的输入时不会误报。我对所有的输入做出不同的功能,但是当我插上这一切到我computepay功能它告诉我:TypeError:float()参数需要是一个字符串或数字

TypeError: float() argument must be a string or a number

def mainloop(): #Creating a loop so it doesn't error out. 
    response = input('Make another calculation?[y/n]') #inputing loop 

    if response == 'n': #creating input for n 
     return 

    if response == 'y': 
     computepay() 
    else: 
     print("\n") 
     print ("Incorrect input. .") 
     mainloop() 
def getname(): 
    name = input ("What's your name?") #input of name 

def getwage(): 
    wage = input ("Hello there! How much money do you make per hour?") #input 
    try: 
     float(wage) #Making it so it does not error out when a float 
    except: 
      print ("Bad Input") 
      getwage() 

def gethours(): 
    hours = input ("Thanks, how many hours have you worked this week?") 
    try: 
      float(hours) #Making it so it does not error out when a float 
    except: 
      print("Bad Input") 
      gethours() 

def computepay(): 
    name = getname() 
    wage = getwage() 
    hours = gethours() 

    if float(hours) > float(40): 
      newhours = float(hours) - float (40) #figuring out the amount of overtime hours the person has worked 
      newwage = float (wage) * float (1.5) #figuring out overtime pay 
      overtimepay = float (newwage) * float (newhours) #calculating overtime total 
      regularpay = (float(40) * float (wage)) + overtimepay #calculating regular and overtime total. 

      print (name,",you'll have made $",round(regularpay,2),"this week.") 
    else: 
      total = float(wage) * float(hours) 
      print (name,",you'll have made $",round (total,2),"this week.") 

    mainloop() #creating the loop. 

#------------------------------------------------------------------------------- 

computepay() 
+0

看到发生了什么事情有点令人困惑,因为代码格式化了它,但错误消息为您提供了寻找内容的提示 - 'TypeError:float()参数必须是字符串或数字'。在你浮动的一个呼叫中(例如浮动(工资)),你传递的东西(比如工资)不能转换为数字。 我建议你打印出每个变量,看看里面有什么东西 - 所以,在'wage = getwage()'之后再增加一行'print(wage) – 2014-10-11 00:45:42

回答

1

这些功能都不是返回任何东西

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) 
1

错误信息告诉你的是某处(在错误信息部分报告的行号中,你没有向我们显示),你打电话给float并给它一个参数(即,一个输入),既不是数字也不是字符串。

你可以追踪发生这种情况吗?

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

相关问题