Step 1 - User input from the console

In our Amazon Translate example, we have provided the values directly in the code. What if we want to provide the values as user input at the time we run the program?

In this step, we learn how to provide user input when the program is run and pass this to the function.

Create a new file called lab_5_step_1_console_input.py and enter the following code:

import boto3

def translate_text(**kwargs): 
    client = boto3.client('translate')
    response = client.translate_text(**kwargs)
    print(response) 

### Change below this line only ###

kwargs={
    "Text":"I am learning to code in AWS",
    "SourceLanguageCode":"en",
    "TargetLanguageCode":"fr"
    }

def main():
    translate_text(**kwargs)

if __name__=="__main__":
    main()

The input() function

To pass input to our function when we run python lab_5_step_1_console_input.py we need to use a built-in function called input(). This will prompt the user with a message and then wait for them to provide input.

Modify your program to accept user input:

import boto3

def translate_text(**kwargs): 
    client = boto3.client('translate')
    response = client.translate_text(**kwargs)
    print(response) 

### Change below this line only ###

text = input("Provide the text you want translating: ")
source_language_code = input("Provide the two letter source language code: ")
target_language_code = input("Provide the two letter target language code: ") 

def main():
    translate_text(
        Text=text,
        SourceLanguageCode=source_language_code,
        TargetLanguageCode=target_language_code
        )

if __name__=="__main__":
    main()
  • To run the program, enter the following command in the terminal:

    python lab_5_step_1_console_input.py

  • When you run your program you should get the following prompts:

Provide the text you want translating: 
Provide the two letter source language code:
Provide the two letter target language code: 

The program will wait for you to provide a value before asking for the next prompt.

What did we do?

  • We define a variable for each of the required inputs.
  • We used the input() function to provide a prompt for user input.
  • We used the variables as keyword inputs to our function.

What did python do?

  • Python used the input() function to prompt the user for an input.
  • Python assigned the input value to the variable.
  • Python used the variable with the keyword argument.

Using user input in this way has added additional flexibility and simplicity to the program as our user is not having to edit the actual program to get a result. As such the user does not have to have any knowledge of how the program works.

This method is useful when a human user is going to be providing input at a command prompt. It is less useful for input provided by a program, method, API or in a script. We will tackle that next.

Next Steps

Next, we are going to look at how we can pass our arguments as inputs using parameters that we specify on the command line.