~ User Input - taking string input from a user


4-Obtaining user input (String/Text)

User Input - Obtaining string (text) user input

So far we have been declaring variables such as x and name, and giving them values in the program itself. For instance x = 3 or name="Joe". What if we wanted the user to decide the input? That's what normally takes place in programs so that's what we are going to be looking at. When you log in to facebook or instagram, the first thing you do is INPUT your username and password. This sort of input is string input and is distinct from integer (number)input. Let's look, in this session, at obtaining string input from the user.

Download Python 3 here:



Task 1

1.First, run the code to see how it works! This is a function called verify that says some stuff and then asks the user for their secret password. If the password is a match, then it prints access granted, else, access denied.

2. Can you spot what line the user is being asked for string input?

3. Change the variable 'password' to name, and adapt the program so that only if the user enters the name "Joe", is access granted.

Code

import time

def verify():
  print("===CHECKING IDENTITY=====")
  time.sleep(1.1)
  print(".....please wait while we do important stuff....")
  time.sleep(1.1)
  print("-----Are you Ready----?")
  password=input("Enter the secret password:")
  if password=="open123":
    print("Access Granted")
  else:
    print("Access Denied")
  verify()
  
verify()

Task 2

1.Run the code below and note that we are creating a simple chatbox - a real chatter box! It asks your name, favourite food and favourite word.

2. Add a variable called 'movie' and ask the user for input of their favourite movie.

3. Print a response where it mentions their favourite movie and makes a comment about it. e.g. , I like that movie too.....

4. Now create the functionality for user input for favourite subject. Create the variable called subject, and ask the user for a value to put into it. Respond by mentioning the subject that they enter, and how much you like or hate it too!

Code

def chatbot():
  print("WELCOME TO THE CHATTERBOT")
  name=input("What is your name?")
  print("Nice to meet you "+name)
  food=input("What is your favourite food, if you don't mind me asking:")
  print(food+"...wow....mmm.....I quite like "+food+" too")
  favword=input("Tell me your favourite word and I'll repeat it five times, just because I can!:")
  print(favword+favword+favword+favword+favword)


chatbot()

Task 3

1. The program below creates a simple username based on two user inputs.

2. Ask the user for their last name and for another four digit number. Create a username which is more complex based on AGE + FOUR DIGIT NUMBER + LAST NAME + FIRST NAME (in this order)

Code

def username():
  print("====Enter details and we will generate a username for you=====")
  firstname=input("Enter first three letters of first name>>")
  age=input("Enter your age e.g. 15")
  print("Your username is:"+age+firstname)



username()

Task 4

1.When you are printing the result of variables, it is important to get the spaces right. You'll note that there are two examples below that print the users first name and last name, one without the correct spacing, and one with!

2. See if you can fix the code on line 7, which seeks to print all three names, but with the correct spacing in place! Use the previous example as a guide to help you and remember you can always refer back to the video presentation.

Code

def main():
  lastname=input("Enter Last Name:")
  firstname=input("Enter First Name:")
  print("Nice to meet you"+lastname+firstname)#this prints the name all sqaushed together (no spaces)
  print("Nice to meet you "+firstname+" "+lastname)#this has the right amount of spaces in it
  middlename=input("Enter Middle Name:")
  print("Nice to meet you"+lastname+firstname+middlename) #CAN YOU FIX THIS?
main()

Task 5

1.Run the code below and note that it asks for the user's password but then comes up with an error. The point of the program is to ask for the user's USERNAME first and then their PASSWORD. If the username="hello" and password="open123" then access is granted and they jump to the main facebook function.

2.See if you can replace the comment with a request for user input (in this case to ask the user for their username)

3. Extend the program by asking the user for their age, no. of friends, and gender once they are in the facebook function. This information can then be printed to the screen for verification. e.g. Thank you for confirming your age is ___, no of friends are_____ and gender is______. Note that you cannot declare a variable name with spaces in it. (e.g. no of friends would have to be no_friends

Code

def login():
  print("====LOGIN SCREEN===")
  #Delete this comment and add your code here to ask the user for their username
  password=input("Enter Password:")
  if username=="hello" and password=="open123":
    print("Access Granted")
  else:
    print("Access Denied")

def facebook():
  print("Welcome to Facebook")
  print("====You logged in succesfully!!!!======")
  
login()

Final Challenge

Use your knowledge and skills from the completed tasks. Use the trinket below to complete your challenge.

A company is running a motivational club for students in a primary school and wants to generate a fun username for them so each of them has this to login to the system and on their badges. Write a program that asks the students to enter a)their favourite number b) their favourite animal and c) their first name. Create and print a username to the screen based on their number, animal and first name. e.g. If their number was 7, animal was 'cat' and first name was 'Bob', the username would be: 7BobCat. Note: #note you do not need to convert number here to an integer as you don't need to perform any calculations with it, but rather treat it like a string!

Note: You can print the page, screenshot, write the code in your book, or show the teacher your page.

Answers

Coming soon!