Discussion

Left ArrowBack to discussions page
mdjuricmdjuric Posts: 5 Apprentice
I'm having some trouble with if statements. So I need my program to accept two inputs, and based on those inputs do a certain action. 

At first, my program asked for 1 condition and it would perform the corresponding action. I used a variable input to prompt the user to enter the condition. A simple breakdown of the original program looks something like this:

part_number='enter part number'
     If part_number=1
         *performs action*
     If part_number=2
         *performs action*
     If part_number=3
         *performs action*

I now need to add a second condition. I thought this would be as simple as adding another variable input under each of the conditions the part_number input, but the pendent does not allow me to nest conditions under each other. I'm sure I'm missing something very simply here. To better explain what I'm trying to do, I would like 2 different actions performed on 3 different pieces. Altogether there would be 6 cases in total. 

part_number='enter part number'
action='select action'

Piece 1: action 1 or 2
Piece 2: action 1 or 2
Piece 3: action 1 or 2

Best Answer

  • LoïcLoïc Posts: 22 Handy
    Accepted Answer
    You can put an "If" command in an other one.

    part_number='enter part number'
    action='select action'

    if Part_number = 1
      if action = 1
        *your action*
      if action = 2
        *your other action*
    if Part_number = 2
      if action = 1
        *your action*
      if action = 2
        *your other action*

    But what I would suggest is the use of the function "Switch - case" and create a combination  of your two variables with a third one. For example:

    part_number='enter part number'*10
    action='select action'
    my_case=part_number+action

    switch my_case
      case 11
        *your first action for part 1*
      case 12
        *your second action for part 1*
      case 21
        *your first action for part 2*
      case 22
        *your second action for part 2*
      case 31
        *your first action for part 3*
      case 32
        *your second action for part 3*

    With this combination you have only 10 actions per piece but you can have lots of possibilities and the program would be cleaner.

    Hope this will be helpfull.

    Regards,

    Loïc


Comments

Sign In or Register to comment.
Left ArrowBack to discussions page