Creating the Age App in PyCharm

Creating your first app in PyCharm is extremely easy.

In your newly-created project, just create a new Python file (right-click on the project), and call it something reasonable, like app.py.

The .py extension tells PyCharm this is indeed a Python file. PyCharm also works with a number of other file types, like HTML, CSS, JavaScript, and more.

Inside your app.py file, you can write your code for the age application.

This is the code we wrote in IDLE:

>>> def get_user_age():
        user_age_string = input("Enter your age: ")
        user_age_int = int(user_age_string)
        return user_age_int
>>> def calculate_age_in_seconds(age_years):
        return age_years * 365 * 24 * 60 * 60
>>> def run():
        print("Your age in seconds is {}.".format(calculate_age_in_seconds(get_user_age())))
>>> run()

And we can write that in PyCharm like so, all in our app.py file:

def get_user_age():
    user_age_string = input("Enter your age: ")
    user_age_int = int(user_age_string)
    return user_age_int
def calculate_age_in_seconds(age_years):
    return age_years * 365 * 24 * 60 * 60
def run():
    print("Your age in seconds is {}.".format(calculate_age_in_seconds(get_user_age())))

run()

Notice how the run() method execution (not the definition) is at the end. This is because at the end the methods have already been defined.

If we placed the run() execution at the top of the file, Python would try to execute that method before having created the method.

For example, the code below would not work, because we would execute run() before defining the run method:

def get_user_age():
    user_age_string = input("Enter your age: ")
    user_age_int = int(user_age_string)
    return user_age_int
def calculate_age_in_seconds(age_years):
    return age_years * 365 * 24 * 60 * 60

run()

def run():
    print("Your age in seconds is {}.".format(calculate_age_in_seconds(get_user_age())))

Then, right-click your app.py file and press "Run". This will prompt you to enter your age, and then will give you a result!

results matching ""

    No results matching ""