Activity #1: User Input

In this activity, you will practice collecting user input from the console and storing the data in different variables.

Python uses the input keyword to prompt the user for information. This allows you to build some simple interactivity into your programs.

Tip

For best results, complete the Data and Variables chapter before starting this activity.

Setup

  1. Open your code editor. Create a new file called user_input_practice.

  2. Define three variables: word, whole_num, and decimal_num. Assign word a string value, whole_num an integer, and decimal_num a a float value.

  3. Add three print statements to output the following text. Fill in the blanks with the values of the variables.

    1. The variables have values of: ____,   ____, and   ____.

    2. Repeat word a whole_num amount of times. For example, if word = 'Hello' and whole_num = 3, print HelloHelloHello.

    3. Display the result of a simple calculation (+, -, *, /) between whole_num and decimal_num.

  4. Run your code to make sure your print statements behave as expected.

Add One Input

Instead of assigning word a hard-coded string, we want to ask the user to type in that information.

To collect user input from the console, Python uses the input keyword.

  1. Replace line 1 with the following syntax:

    1
    word = input('Enter a word:')
    

    The string inside the parentheses () will be displayed in the console. Unlike a print statement, the program will display the prompt and then wait for the user to tap the Enter key before continuing.

  2. Run your program to check that it correctly collects and prints the entered word. You should NOT need to update your print statements.

  3. Note that the prompt displayed in the console does not include any space after Enter a word: unless you added it in the code. TRY IT!

    1. Compare the prompts 'Enter a word:' and 'Enter a word: '.

    2. To collect user input on the line below the prompt, add the newline character \n to the end of the string:

      1
      word = input('Enter a word:\n')
      
    3. What does the \t character do in 'Enter \t a \t word:\t'?

  4. The examples in step 3 show different ways of using whitespace to change the look of the output in the console.

Whitespace - Good idea!

Numerical Inputs

Replace the hard-coded number for whole_num with an input statement. Use the prompt, "Enter a whole number: ".

When you run the program, you get an error message! This occurs because Python treats all input data as the string data type. To correct this, you need to convert the input to the int data type. You can do this with two lines of code, or you can combine these into a single command.

Example

Two Lines:

Store the input in the variable, then use the int() function to convert from the string data type to the integer data type.

2
3
whole_num = input("Enter a whole number: ") # String data type, like "3".
whole_num = int(whole_num)                  # Now an int data type, like 3.

One Line:

Place the input statement inside the int() function.

2
whole_num = int(input("Enter a whole number: "))
  1. Using either of the options shown above, update whole_num to store the user’s input as an integer data type.

  2. Run your program to check that it correctly collects and uses the entered number. You should NOT need to update your print statements.

  3. Replace the hard-coded value for decimal_num with an input statement. Use the prompt, "Enter a decimal number: ". You will need to convert the input to the float data type.

  4. Run your program again to make sure it still works.

Submit Your Work

Follow your teacher’s instructions on how to submit your work for review.