使用来自用户输入的数据进行整数线性编程

时间:2018-04-23 03:03:05

标签: python linear-programming

我试图解决线性编程问题,但是当我从用户输入插入数据时我陷入困境。我试图将数据输入到字典中以获取每种产品的利润,但是代码有错误说明TypeError:不支持的操作数类型为+ =:' int'和' str'



# Creates a list of the Products and Services
Products_Services = ['Herbal Products', 'Radix Jumbo', 'Radix IQ', 'Patients']

# User-input data
HP_profits = input("\nEnter the profit from one bottle of Herbal Products: ")
RJ_profits = input("Enter the profit from one pack of Radix Jumbo: ")
RIQ_profits = input("Enter the profit from one pack of Radix IQ: ")
Pt_profits = input("Enter the profit from one patient from one session of cupping therapy: ")
       
# A dictionary of the profits of each of the Products and Services is created
profits = {'Herbal Products': HP_profits,
           'Radix Jumbo': RJ_profits,
           'Radix IQ': RIQ_profits,
           'Patients': Pt_profits}

    # Create the 'prob' variable to contain the problem data
prob = LpProblem("FOM Enterprise Profit Problem", LpMaximize)

# A dictionary called 'no_prodserv' is created to contain the value of referenced Variables and set it to integer
no_prodserv = LpVariable.dicts("Number of",Products_Services, cat = 'Integer')

# The objective function is added to 'prob' first
prob += lpSum([profits[i]*no_prodserv[i] for i in Products_Services]), "Total profit of FOM Enterprise in a month is "




感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

想想错误:

  

TypeError:+ =:'int'和'str'

不支持的操作数类型

您正在尝试将字符串添加到整数

您的代码中只有一个“+ =”实例,即:

prob += lpSum([profits[i]*no_prodserv[i] for i in Products_Services]), "Total profit of FOM Enterprise in a month is "

这个表达式显然是评估为字符串。

PS:在上面的表达式中你有一个元组(函数lpSum返回的某个值和由逗号分隔的字符串),如果没有特别重载,你就不能在不同的数据类型上使用“+”运算符(元组和整数在这种情况下不是)。

hello = 123, "abc"
world = 1

print(type(hello)) # <class 'tuple'>
hello += world # TypeError: can only concatenate tuple (not "int") to tuple

基于上面的“测试”,我的猜测是,在作为示例提供的代码中没有发生给定的错误......