Milandeep Bassi

Taking Inputs from Users in Python Programming

17th, January, 2020


Taking Inputs from Users

We have now learned how to use variables to assign data types and to manipulate this data and print it. However something to take not is that you can print variables and do operations at the same time.

>>> print ( 2 + 8 )
10

Doing this is quicker than assigning a variable and then printing out the variables. However it is possible to make complex maths print out simpler and make the code more efficient for example below. 

>>> cars_counted_total = 36
>>> days_taken = 3
>>> average = cars_counted_total / days_taken
>>> print ( average )
12

Laying out the code like this makes it easier in the future to change the data. 

We can also allow users to enter there own data into a variable for storage. 

variable = input()

This is the format that we will use to allow the program to request inputs from the user. However the user needs to know you're prompting them for an input so in your program you would do this.

name = input ( "Please enter your name: " )

Since we're asking the user just for the name we can also use what you learnt earlier in the data types to request specific data types from the user input. 

name = str ( input ( "Please enter your name: " ) )

This will ensure whatever is enter will be stored with "x". We can also then use this request only numbers.

age = int ( input ( "Please enter your age: " ) )

Same can then be done with the float data type.