Basic user input (Text Based)

--Originally published at Quirino´s Projects

When creating console programs, in python or any other programming language,  we have a few options when it comes to collecting data from the user, a really helpful one is input().

input is a function that prints to console the argument given to it, then enables user input and returns it as a string

Useful examples

name = input(“What is your name “)

#”Whats your name gets printed to the console”

#input() Returns the string that the user gave

The example above gets stored as a string, this is useful when we want to work with them, but becomes problematic when we want to work with numbers, in that case we can use int()

num = int(input(“Give me a number “))

#The string is converted to an integer

NOTE: We should always expect that the user will try to mess with our program, check my post about Validating user input for this

Using the input() function can be used for checking what the user wants to do inside our program

def converter(userInput):

if userInput() == “C”:

convertToC(input(“Give me the temperature”))

if uInput.upper() == “F”:

convertToC(input(“Give me the temperature”))

else:

print(“Invalid input”)

converter(userInput)

Also input() can be very practical to create other programs, check my TicTacToe.py in my GitHub https://github.com/QuirinoC/TicTacToe.pycaptura-de-pantalla-2016-10-28-a-las-09-08-41


Basic Types and Their use

--Originally published at Quirino´s Projects

maxresdefaultThere are basic python data types that python includes, these are very useful when you want to work with data, some of these are:

int

Includes whole numbers negative and positive

-10 -9 -8…8 9 10

When one wants to convert some string that contains a number we can use the Python Built-In Function int()

A really practical use of the int() function is to convert the user input to a integer, since input() returns a string

num = int(input(“Give me a number”))

print (num*5)

Without the int the print would return the number 5 times as a string

For example if the user input was 35, instead of returning 175, it would have returned “3535353535”

 

 

 

float

Positive and negative decimal numbers

5.2532 4.546 5.0

If one wants to convert a string to float we would use the Built-In function float()

 

 

Note:

Passing something that cannot be converted to int or float using the functions explain above will stop execution, check my post about Validating user input on how to make sure user inputs correct information

bool

The boolean data type can only contain two different values, true or false, this one is useful for boolean operations

There are many ways to get a True or a False value, some of these are:

Comparison

> Compares if the first value is bigger than the second one
6>5 True

5>6 False

< Compares if the first value is less than the first one

5<100 True

10<10 False

>= Bigger or equal checks like bigger than but also if the value is bigger

50>=50 True | 50 > 50 False

<=Less or equal checks like bigger than but also if the value is lower

50<=60 True

== This comparison operator can also check for more values, not

Continue reading "Basic Types and Their use"

Validated user input

--Originally published at Quirino´s Projects

When we need to read input from the user we must know that sometimes the user won’t know exactly what data type you want, for example if we had a program that asked for how many apples 

 nApples = int(input("How many apples do you want?")) 

This gets the number of apples the user wants and stores in a variable called nApples, but the problem comes when the user gives data that is not convertible to int like a string or a float that can’t be converted to a integer, of course we could explicitly say that we only accept integers but still this would stop the user from trying to crash our program in this case i would do the following

def onlyInt():#Were question is the string to be asked
    uInput = input(" ")
    try:
        return int(uInput)
    except:
        print ("Your input is incorrect, retry please")
        onlyInt()
onlyIntVar = onlyInt()

 

This function will pass only an integer to the variable

Or we could do something like

def onlyInt():
    uInput = input(" ")
    if not type(uInput) == int:
print("Invalid input")
onlyInt()
return uInput

 

This way we can be sure that the program won’t crash because errors like this

 

 


Creation and use of ranges

--Originally published at Quirino´s Projects

When you want to get a set of the range from number ‘a’ to number ‘b’

range() Is the function you might want to use. To create it one argument is enough, we could simply do:
range(5)

This creates a set from 0 to 5

This argument is called ‘Stop’, indicates where to stop, since we have no ‘Start’ argument, python takes ‘0’ as the starting number

If we want to start from another number we need to pass two arguments to the function

range(6,22)

This creates a set from 6 to 22

Now that range is taking two parameters, the first one is ‘Start’ it defines where the range will start and the second will be ‘Stop’

The third argument the range function can take is ‘Step’ this indicates the step the range, for example

range(0,100,2)

Will create all the even numbers from 0 to 100

 

The range function can be treated like a list but its still a range object

The most common use i have found for ranges is to do something n times in an object with a for loop with the index of the object each iteration.

Example

If i wanted to work with the index of the string “Juan”

using

for i in string:

would return J u a n

Instead i could use

for i in range(len(juan))

This would return 0 1 2 3