Sunday, January 15, 2017

Python Input from User

Howdy!

Before going on, I assume:

1. You've installed PyCharm
If you haven't, please see this first.
Installing Python IDE:
https://umpythonworkshop.blogspot.my/2017/01/installing-python-ide.html

2. You know how to open up a new file in PyCharm.
If you don't know, please see this first.
Python Basics:
https://umpythonworkshop.blogspot.my/2017/01/python-basics.html



If you fulfill the above assumptions, continue!


***

WHAT WILL WE DO IN THIS POST?
- Learn Python input and output.


Suppose we wanted an app to greet user.
We want the user to type his/her name,
then say hello to him/her.

just type these two lines in PyCharm Editing Box.

user_name = input("What is your name?")
print("Hello there,", user_name)

WHAT DOES THESE 2 LINES MEAN?

First, i ask the computer to ask user for input,
and i store the input in user_name



THE FIRST LINE:
user_name here is called a variable.

A variable is like a box.

A box can store anything.
A box can store a kitten, a dog, a number, your age, some words, your name, a whole paragraph, ...
In our case, user_name is used to store user's name - which is a few words.


(A box can store kittens, numbers, decimals, words, paragraphs)

input() is a method to ask the user's name.
Between the parenthesis (), you can have some words to ask the user.
In this case, it's "What is your name?"



THE SECOND LINE:
We've discuss the method print() in our previous post.
Previous post (Python basics) : https://umpythonworkshop.blogspot.my/2017/01/python-basics.html



If you've noticed, there is quotes ("") in "Hello there," ;
But no quotes ("") in user_name.

Because "Hello there," is a string ;
And user_name is a variable name.


IMPORTANT: What is a string?
A string is a data type any character or words or sentence or paragraphs enclosed in quotes ("").
You'll learn more about data type in


and run it using  ALT + SHIFT + F10  and hit  ENTER 

And in PyCharm Run Box, you'll see this.




Try to type in your name, and hit  ENTER 




VOILA!

A talking app!

Try to change a few things on your own.

user_name = input("What is your name?")
print("Hello there,", user_name)

You can change the above lines to

age = input("How old are you?")
print("You are", age, "years old.")

and run it using  ALT + SHIFT + F10  and hit  ENTER 




1 comment:

  1. Hire php developers – Save cost by hiring dedicated PHP developers & PHP programmer from this site.

    ReplyDelete