What is not efficient about this?
You can retype this code in a lot of ways but none of them have anything to do with efficiency. The bigger question is why you are even concerned about this.
Don't get lost in trying to make things efficient especially if you are a beginner.
Edit
Method 1
If you have a scenario where you have multiple prompts the you could think of getting rid of if statements and setting up an interface like a dictionary that maps each user's prompt response to a function that handles that specific prompt that what you would just have something like
def PrintHello():
print('Hello')
LookUpFunction = {'hello':PrintHello}
def HandleUser():
try:
return LookUpFunction[input('What would you like to do ? ')]
except KeyError:
return None
while True:
func = HandleUser()
if(func):
func()
break
Method2
You could also just create functions that are name aliases for the input string that the user provides and then use locals
or globals
to call the function like this.
def c_hello():
print('Hello There')
def c_quit():
print('Goodbye')
command = locals()['c_'+input('What would you like? ')]
command()
so all you have to do in both this cases is provide the functions that handles the particular Prompt