Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 13 exercise 2 #8

Open
tae898 opened this issue Sep 28, 2021 · 1 comment
Open

Chapter 13 exercise 2 #8

tae898 opened this issue Sep 28, 2021 · 1 comment

Comments

@tae898
Copy link
Contributor

tae898 commented Sep 28, 2021

Original question:

Chapter 13 exercise 2
Hello, 
For me it is still not very clear how to fix this exercise. Could you give me an explanation on how to do this?
Thank you so much! 

@tae898
Copy link
Contributor Author

tae898 commented Sep 28, 2021

Hi there,

Exercise 2 of Chapter 13 is about creating two python files.

One is a module to be imported (i.e., my_utils.py) and the other is a script that you’ll run directly (i.e., my_second_program.py)

Yesterday during the lecture I created the two files as below:

my_utils.py:

import os
import datetime
from random import randint

def get_current_directory():
    """Return the current working directory"""
    to_return = os.getcwd()
    return to_return

def get_current_time():
    """Return the current time."""
    return datetime.datetime.now()

def get_random_int(low: int, high: int):
    """Sample a random integer from the uniform 
    distribution (low,high)."""
    return randint(low, high)

foo = 'foo'
bar = [1,2,3,4]

my_second_program.py:

import my_utils

print(my_utils.get_current_directory())
print()
print(my_utils.get_current_time())

from my_utils import foo, bar

print(foo, bar)
print()

from my_utils import get_random_int
print(get_random_int(-100, 100))

Make sure that both python files are in the same directory and your current directory in your terminal
is also the directory where the two files are located.

Run python my_second_program.py to see that this script successfully imports the module my_utils,
and do stuff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant