【问题标题】:i ran into this error syntax error and i couldn't figure out any other solution with out ruining it [closed]我遇到了这个错误语法错误,我无法在不破坏它的情况下找出任何其他解决方案[关闭]
【发布时间】:2022-11-17 22:55:43
【问题描述】:

我遇到了这个错误语法错误,我无法在不破坏它的情况下找出任何其他解决方案

#function for calculating revenue
def revenue(x):
    return 0.05x+2.5

#function for calculating cost 
def cost(x):
    return 0.03x

#opening the txt file in read mode which contains the sales data
sales = open("data.txt", "r") ;

total_revenue=0;
total_cost=0;

#reading the sales data line by line 
for x in sales:
    #calculating total revenue
    total_revenue+=revenue(int(x))
    #calculating total cost
    total_cost+=cost(int(x))

#calculating profit 
profit=total_revenue-total_cost
#printing the profit
print(profit)
#closing the sales file
sales.close() 

输出应该是:-

(基础)aky03@Ajits-MacBook-Air 100ct % python main.py

40.7 但它不工作

【问题讨论】:

  • 0.5x 之类的东西不是编写产品的有效方式。使用0.5*x。您在整个代码中系统地执行此操作。
  • 编程不是数学。您不能编写 0.5x 并期望它执行乘法:您必须实际使用乘法运算符 0.5 * x

标签: python


【解决方案1】:

return 0.05x+2.5 -> return 0.05 * x + 2.5

return 0.03x -> return 0.03 * x

同时删除;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多