S
S
Sergey Karbivnichy2015-12-13 14:36:51
Python
Sergey Karbivnichy, 2015-12-13 14:36:51

Python. Keyboard input?

Let's say we have this code:

def func1():
    print("Func 1")

def func2():
    print("Func 2")

def func3():
    print("FUnc 3")

command = input("-->")
if command == "1":
    func1()
elif command == "2":
    func2()
elif command == "3":
    func3()
elif command == "exit":
    exit()
else:
    print("Ошибка. Введите команду еще раз!")

How to make it so that after entering a command and pressing enter, the corresponding function is executed, and then again it requires keyboard input, and the program ends only when the user enters the exit command?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
coderisimo, 2015-12-13
@coderisimo

while command != 'exit' :
      command = input("Введите команду ") 
       //что-то происходит в зависимости от команды

general idea)

Z
zaswed, 2015-12-16
@zaswed

def func1():
    print("Func 1")

def func2():
    print("Func 2")

def func3():
    print("FUnc 3")

commands = {'1': func1, '2': func2, '3': func3}

repl = None
while repl != "exit":
    repl = input("Введите команду ")
    if repl in commands:
        commands[repl]()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question