-1

When I make a choice, the code is executed normally. But when I want to redo the choice later nothing happens. Example: I choose 1. The code is executed. I come back to the menu. I want to rechose 1, but nothing happens and the menu is displayed again.

version : Python 3.3.0

menu = True
while menu:
    try:
        c = 0
        c = int(input("Choose 1,2,3,4,5 or 6"))
    except:
        print("Veuillez choisir un chiffre entre 1 et 6!")

    if c == 1:
        c = 0
        #some code

    elif c == 2:
        c = 0
        #some code

    elif c == 3:
        c = 0
        #some code

    elif c == 4:
        c = 0
        #some code

    elif c == 5:
        c = 0
        #some code

    elif c == 6:
        menu = False

    else:
        print("Veuillez choisir un chiffre entre 1 et 6!")
martineau
  • 119,623
  • 25
  • 170
  • 301
hug0
  • 15
  • 6
  • 2
    Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – martineau Jun 04 '19 at 23:08
  • If the answers to the linked question don't resolve your problem, please flesh out your example code so that we can run it ourselves to see the same behavior you are asking about. The easiest thing would be to replace `# some code` with `print(c)`. – Code-Apprentice Jun 04 '19 at 23:12
  • Nothing happens because your processing blocks don't do anything lasting. You set `c` to 0 and loop back to the top to replace that `0` with the next input value. – Prune Jun 04 '19 at 23:19

1 Answers1

0

It is not a good approach to use IF statements. It won't execute a couple of times for each case because you havent defined it, e.g. using for loop. It would be great to have '''switch''' in Python. Instead of that you can create dictionary. Or something like below:

def zero():
    return "zero"

def one():
    return "one"

def two():
    return "two"

switcher = {
    0: zero,
    1: one,
    2: two
    }


def numbers_to_strings(argument):
    # Get the function from switcher dictionary
    func = switcher.get(argument, "nothing")
    # Execute the function
    return func()

Input: numbers_to_strings(1)
Output: One

Input: switcher[1]=two #changing the switch case
Input: numbers_to_strings(1)
Output: Two
Marta
  • 37
  • 10