Sign in or Sign up

Python Series #2: User Input
Started by Bloodhound


Rate this topic
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5


2 posts in this topic
Bloodhound Offline
Python Enthusiast
****


Diamond
Posts: 105
Threads: 14
Joined: Thu Apr 2017
Reputation: 2

CZPoints: 6 CZP
ContributorDiamond
04-15-2017, 09:03 AM -
#1
Python Tutorial Series Issue #2: User Input


Thanks for reaching my second issue, I appreciate the time you spend reading and I hope you are learning something. In this issue we will cover: User Input and Casting Data.

User input is very simple to obtain, like easily simple. Thank’s to Python’s Pseudocode like language, you wont have a hard time remembering most of these functions we will cover. So to start with we will make a program that will ask the user for their name and age and print a nice welcome statement. 

So we start with you need to name a variable to store our input, let’s name it ‘user_name’, once we do that we then need to assign a value, which is our input, here I’ll show you how to do this:
Code:
user_name = input(“Hello User! What is your name?\n”)

So here is our statement. We have named our variable, assigned the input as our value, and within the input() function we have included text which the user will type their name next to. And of course you may be frightened when you see \n but it is nothing to worry about. All it does is tell Python that you want a newline. So when you run this the user will input their name on the line below this question. You don’t have to do this but I like to think it makes my program cleaner. 


Now that we have our user’s name, we need to get their age. So let’s create a variable, assign a value and run our code. So we do:
Code:
user_name = input(“How old are you?/n”)

Now we have done this, there is something you need to know. By default, the input() function converts the input data into a string so if we inputted 19 as our input:
Code:
user_name == “19”

Now this is impractical as our number is stored as a string. This is why we ‘cast data’. All this is, is converts one data type to another. For example we want to convert user_name into an integer, there is two ways we can do this:
Code:
int(user_name)

or in the initial input statement we can put:
Code:
user_name = int(input(“How old are you?/n”))

What this means is the entire output of the input() function is casted into an integer. So let me show you the process:
1. The input function outputs the entered text
2. The user enter’s their age (17)
3. The age is first stored as a string (“17”)
4. The age is then casted to an integer (17)

Types of casting:
- int() converts to an integer
- str() converts to a string
- bool() converts to a boolean
- float() converts to a float

Now let’s welcome the user, this will show you how to print variables, and multiple variables in one statement, so here’s how:
Code:
print(“Welcome, user_name, “you are”, user_age, “years old”)

as you see when printing variables they are not within quotation marks, there is a way how to do this but that’s for a later issue. The reason we use a comma to separate the variable is because they are of different data types, user_name is a string and user_age is a integer, if they were the same, we could have concatenated them into one string using a + symbol. If you notice I also didn’t include spaces between the parts of the statement, this is because the comma automatically adds a space for you. Although concatenating them doesn’t so you would have to put them in manually. I will show you a way to concatenate them using the casting methods we learnt:
Code:
print(“Welcome “ + user_name + “ you are “ + str(user_age) + “ years old”)

Here I have included my spaces manually, and of course I concatenated user_age to the string. However without casting it to a string, when trying to concatenate it you will receive an error.

Here is our final program:
Code:
user_name = input(“Hello User! What is your name?\n”)
user_age = int(input(“How old are you?\n”))
print(“Welcome”, user_name, “you are”, user_age, “years old”)

There you go, new issue will be out in a few days as I have a long flight to catch tomorrow. Leave feedback and I’ll see you later!
[Image: rElVjPf.png]
ScorpionOfWar Offline
Super Moderator
******


Super Moderators
Posts: 239
Threads: 11
Joined: Fri Mar 2017
Reputation: 14

CZPoints: 38 CZP
Contributor
04-15-2017, 09:05 AM -
#2
As always a hq post! Thank you again for your high quality posts and all your contributions
[Image: PWXNX5p.gif]

Discord - @ScorpionOfWar#0001
Bloodhound Offline
Python Enthusiast
****


Diamond
Posts: 105
Threads: 14
Joined: Thu Apr 2017
Reputation: 2

CZPoints: 6 CZP
ContributorDiamond
04-15-2017, 09:35 AM -
#3
Thanks for the support
[Image: rElVjPf.png]




Users browsing this thread: 1 Guest(s)