我试图在3台机器之间随机编码。
因为Python没有任何switch/case函数,所以我求助于if
和{
每台机器(line1(),line2(),line3()
)也有自己的功能。
但是,我一直在犯错。在
请告诉我出了什么问题。在
machine_choice = [1,2,3]
selected_machine = random.choice(machine_choice)
print(selected_machine)
def machines(selected_machine):
if selected_machine == 1:
print("machine 1 randomly selected")
line1()
elif selected_machine == 2:
print("machine 2 randomly selected")
line2()
else:
print("machine 3 randomly selected")
line3()
machines(selected_machine)
def line1():
if machine1["hardness"] < rev[i][1]["condition"]:
print("\tMachine 1 hardness breached, will take 30mins for changing the system")
machine1["hardness"] = 10
time = line1
machine1["schedule"].append([d,"machine 1",time,"change",30.,time.add(minutes=30)])
print("\tno activities from {} to {}".format(time,time.add(minutes=30)))
time = time.add(minutes=30)
print("\tdone changing at time: ",time)
print("\tcurrent log: ",machine1["schedule"],"\n")
d+=1
line1=time
#line1 = max(time,line1,line2)
machine1["schedule"].append([d,"machine 1",line1,"feeding",5.,line1.add(minutes=5)])
line1 = line1.add(minutes=5)
d+=1
machine1["schedule"].append([d,"machine 1",line1,rev[i][0],rev[i][1]["duration"],line1.add(minutes=rev[i][1]["duration"])])
line1 = line1.add(minutes=rev[i][1]["duration"])
time = time.add(minutes=5)
d+=1
if rev[i][1]["quantity"] == 0:
i += 1
该消息意味着您试图访问某个定义为函数的python对象,就好像它是一个数组一样。最有可能的候选代码与您提供的代码是machine\u choice。在
但请提供一个最小工作示例,在这种情况下,这意味着包括导入必要的模块(随机)和行函数。在
对于代码的目的(伪造开关),级联if解决方案通常不是一个很好的解决方案。使用随机选择如果使用数组、字典或以随机数作为键或索引的数组,则会更像python。在
在OO编程中,使用switch也是一种常见的代码味道,通常最好使用对象多态性和工厂来代替它。在
您可以查看python switch replacement了解详细信息。在
在Python中,函数没有什么特别的,可以像使用其他对象一样使用它们。在
相关问题 更多 >
编程相关推荐