DEV Community


Posted on


Learning Python - Week 3

This week I continued to learn the control flow in Python from last week. What I want to focus on in this post is accepting the input from a user. Having a user input information is very easy, all you need is the input function and as the argument for the function you pass in the text or information that you want to see in the console so that the user knows what information they need to put. Here is an example of how to use the input function:

name = input('Enter your name: ')
print("Hello there " + name.strip()) # strip removes any white spaces

age = input('Enter your age: ') # input returns a string not an integer
print(name.strip() + "... you are " + str(age.strip()) + " years old")
print(name.strip() + "... you will be " + str(5 + int(age)) + " in 5 years")
Enter fullscreen mode Exit fullscreen mode

The name variable accepts an input of name and the age variable accepts the input of your age. When you run the program it will say:
Enter your name:
You will enter your name and press enter. The strip function at the end is a way to remove the white space meaning if you happen to add a lot of spaces before you enter your name, it will remove it. Next is the age, when you put your age in it is not an integer, it is a string. So when you press enter it will say
[your name] you are [your age] years old and then it will say [your name], you will be [add 5 to your age] in five years.
This was a quick practice and this is what I really like to do when it comes to programming, having a user experience. I am sure I will be able to have a more advanced way of having a user experience in the future but for now this is what I know. I know this is a short post but it was for a good reason. Week 4 will be a whole new section focusing on packages, modules and object oriented programming. It’s still a fun time learning this and I will talk to you guys next week!

Top comments (0)

πŸ‘‹ New to DEV?

Head over to our Welcome Thread and tell us what you're working on!