2016-04-15 82 views
0

好吧,我试图弄清楚这一点,没有任何东西。我很确定我只是有一个“嘟嘟嘟嘟”的时刻,只要有人看到我的问题,我会觉得愚蠢,但我会问无论如何!我没有得到我以前得到的错误,所以我甚至不知道如何解决它。任何帮助,将不胜感激! :dTypeError:一元运算符类型错误+:'str'第15行?

def main(): 
    # Variables 
    total_sales = 0.0 

    # Initialize lists 
    daily_sales = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] 
    days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', + \ 
        'Thursday', 'Friday','Saturday'] 

    for index in range(7): 
     daily_sales[index] = float(input('Enter the sales for ' + \ 
            days_of_week[index] + ': ')) 

    for number in daily_sales: 
     total_sales += number 

    # Display total sales 
    print ('Total sales for the week: $', + \ 
     format(total_sales, ',.2f'), sep='') 

# Call the main function. 
main() 
+0

你永远需要一个'\'括号内' (...)',括号'[...]'或大括号{...}。事实上,我已经看到它建议你不要使用'\',而是在表达式中使用括号。 – millimoose

+0

@AmberHolcombe您能否请我检查我的答案是否能解决您的问题,如果您能请您验证我的答案吗? – lmiguelvargasf

+0

@AmberHolcombe你解决了你的问题吗? – lmiguelvargasf

回答

0

我似乎无法复制你越来越但是从你的代码,我可以看到你希望用户输入销售的每一天,并获得总误差。这里是我会怎么做

def main(): 
    days_of_week = [ 
     'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 
     'Saturday' 
    ] 

    # use a list comprehension to generate a list of entered values 
    total = [float(input('Enter the sales for %s: ' % a)) 
      for a in days_of_week] 

    # use sum function to calculate the total in the list 
    print "Total sales for the week: $%s" % sum(total) 

main() 
0

你的问题的简单方法是:

print ('Total sales for the week: $', + \ 
    format(total_sales, ',.2f'), sep='') 

具体来说,部分离开这里:

         + \ 
    format(total_sales, ',.2f') 

这行继续\末(不必考虑parens,但删除它不会帮助)意味着你在做+format(total_sales, ',.2f')format返回str,而str没有实现一元+(因为一元+用于数学表达式)。

解决办法是刚刚摆脱了+的:

print ('Total sales for the week: $', 
     format(total_sales, ',.2f'), sep='') 

你也可以使用str.format方法简化了一下:

print('Total sales for the week: ${:,.2f}'.format(total_sales)) 
相关问题