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