Monday, October 1, 2018

Accepting user input to make interactive programs

We have used the print() since our first program and know that it enables a Python program to display textual information to the user. What if the program wants to obtain input from the user?

Python has an inbuilt input() which prompts a user to enter information which is captures into a variable.

user_text = input()

Let's see a simple program input_demo.py

print("Enter your name")

myName = input()

print ("Your name is: " + myName)

Once you run this program you can see the command prompt waits for your input ( screen shot below)




I typed Veevaeck and pressed enter



The  output is shown below -





As you can see my input string Veevaeck was stored in the variable myName which was then used in the print() to display my input.

Remember that the input function produces only strings so you may have to use type casting depending on the type of variable to require. For example if you want to make a program which accepts to integers as user input. add them and display the result. See the program below-

add_numbers.py

print("Enter first number")

num1 = input()

print("Enter second number")

num2 = input()

sum = num1 + num2

print("The sum of entered numbers is: " + sum)


When you run this program the result is as shown below-





As per inputs the expected output was 50 and not 2030. The reason being num1 and num2 are string type and the + operator concatenated the strings and hence gave 2030 as output.

Here is the modified version of the program add_numbers.py

print("Enter first number")

num1 = input()

num1 = int (num1)

print("Enter second number")

num2 = input()

num2 = int (num2)

sum = num1 + num2

print("The addition of entered numbers is: " + str(sum))


The output is shown in the screen shot below





The line num1 = int (num1) converts the string type num1 to integer type similarly we have typcasted num2. Did you notice the type casting str(sum) in the last line of the program ? try to find the need for this. Better run the program by removing the type casting and see the ouput

print("The addition of entered numbers is: " + sum)

We always need a message to get the required input from the user and we normally use a print() to display the message however the input() also accepts a string argument which can obviate the use of print() to display the input message. See the modified version of the program add_numbers.py

num1 = input("Enter first number: ")

num1 = int (num1)

num2 = input("Enter second number: ")

num2 = int (num2)

sum = num1 + num2

print("The addition of entered numbers is: " + str(sum))



 We can also do typecasting while taking input as shown below-

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

 See the modified version of the program add_numbers.py

num1 = int(input("Enter first number: "))

num2 = int(input("Enter second number: "))

sum = str(num1 + num2)

print("The addition of entered numbers is: " + sum)



 Controlling the print()

Let's see our old program

print("Enter first number")

num1 = input()

num1 = int (num1)

print("Enter second number")

num2 = input()

num2 = int (num2)

sum = num1 + num2

print("The addition of entered numbers is: " + str(sum))


 
When we run this program the cursor prompting for the user input is  on the next line after ( see the screen shot) When the user presses the enter key to complete the input, the cursor automatically will move down to the next line. The print function always prints a line of text, and then the cursor moves down to the next line so any future printing appears on the next line.





What if I want my cursor on the same line after the text message "Enter first number" i.e at the end of the printed line?

The print() accepts an additional argument that allows the cursor to remain on the same line as the printed text, so when I write

print("Enter first number", end='')


A print function call of this form will cause the cursor to remain on the same line as the printed text. The expression end='' is known as a keyword argument. The term keyword here means something different from the term keyword used to mean a reserved word.

The print statement

print("Enter first number", end='')

means “Print the message Enter first number, and then terminate the line with nothing rather than the normal \n newline code.” Another way to achieve the same result is

print(end="Enter first number")

This statement means “Print nothing, and then terminate the line with the string Enter first number
rather than the normal \n newline code. The behavior of the two statements is indistinguishable.



You won't be surprised to know that the statement

print("Enter first number") 

is actually  print("Enter first number", end='\n') that is, the default ending for a line of printed text is the string '\n', the newline control code.




Another keyword argument allows us to control how the print function visually separates the arguments it displays. By default, the print function places a single space in between the items it prints. print() uses a keyword argument named sep to specify the string to use insert between items. The name sep stands for separator. The default value of sep is the string ' ', a string containing a single space.

See the program print_demo.py below-


w, x, y, z = 5, 10, 15, 20
print(w, x, y, z)
print(w, x, y, z, sep=',')
print(w, x, y, z, sep='')
print(w, x, y, z, sep=':')
print(w, x, y, z, sep='-----')

The output is shown below -





The first of the output shows print’s default method of using a single space between printed items. The second output line uses commas as separators. The third line runs the items together with an empty string separator. The fifth line shows that the separating string may consist of multiple characters.
 














Share:

0 comments:

Post a Comment