-1

I am trying to code a random decision between 3 machines. Since Python does not have any switch/case function, I resorted to if and elif.

Each machine (line1(),line2(),line3()) has its own function as well. However, I have been getting errors.

Please advise me on what is going wrong.

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
PCiah
  • 27
  • 1
  • 4
  • 1
    Where are `line1` etc. defined? – khelwood Feb 05 '18 at 09:31
  • Why not use a dictionary instead of attempting to implement a `switch`? – iBug Feb 05 '18 at 09:33
  • 3
    Please show us the complete stack trace of the error you get. Otherwise we have to comb through your code looking for the place where it might be. Your error is not in the code you present so must be in one of the functions `line1()`, `line2()` or `line3()`. The stack trace would point that up at once. – BoarGules Feb 05 '18 at 09:33

2 Answers2

2

I am trying to code a random decision between 3 machines.

In Python, functions are nothing special, you can use them just like any other object.

def machine_one():
  return 'Machine One'

def machine_two():
  return 'Machine Two'

def machine_three():
  return 'Machine Three'

machines = [machine_one, machine_two, machine_three]

print(random.choice(machines)())
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
0

The message means that you try to access to some python object defined as a function as if it were an array. The most likely candidate looking as the code you provided is machine_choice.

But please provide a working minimal example, which in that case means including import of necessary modules (random) and lines function.

Regarding the purpose of the code (faking a switch) the cascaded if solution is usually not a very good solution. Using random.choice with an array or a dictionnary or an array with random number as key or index would be much more pythonic.

Also using switch is a common code smell in OO programming and is often best replaced using object polymorphism and a factory.

You may have a look at python switch replacement for details.

kriss
  • 23,497
  • 17
  • 97
  • 116
  • thanks for the explanation. I am using random.choice with array. Since i will be working with 3 machine, i figure it will be more understandable declaring that inside the function. – PCiah Feb 06 '18 at 00:17