2

I'm trying to figure out how to run my functions from my homework file. I opened up terminal, changed the directory to desktop (where my hw1.py file is), started up python, then typed "import hw1". Then I type in fizzbuzz(15) and it returns this statement:

Traceback (most recent call last): File "<stdin>", line 1, in <module>

My only function I'm trying to run is:

def fizzbuzz(n):
 if (n % 3) == 0 and (n % 5) == 0:
 return "FizzBuzz"
 elif (n % 3) == 0:
 return "Fizz"
 elif (n % 5) == 0:
 return "Buzz"
Toms Code
1,7595 gold badges22 silver badges38 bronze badges
asked Sep 14, 2014 at 19:18
1

4 Answers 4

5

import hw1 Then use hw1.fizzbuzz(15)

You can also use:

from hw1 import fizzbuzz

so you will have imported fizzbuzz into your current namespace and can then call it using fizzbuzz(15).

python modules

import hw1 just imports the module name not all the functions defined in it so either use my_module.my_function or the from my_module import my_function syntax.

answered Sep 14, 2014 at 19:20
Sign up to request clarification or add additional context in comments.

1 Comment

No worries, have a peek at the doc examples I linked to, there are nice easily understandable examples.
2

Although this question is about executing a function from a python file (as a Module) inside the Python Interpreter in Interactive Mode, it's also possible to run a function or module with a one-liner (i.e. not use the interactive interpreter), as documented in Command line and environment. Amongst others:

When called with -c command, it executes the Python statement(s) given as command. Here command may contain multiple statements separated by newlines. Leading whitespace is significant in Python statements!

While quotation marks for strings etc. need escaping (depending on your environment), I could get the desired output on Windows 10 with Python 3.8 using:

python -c "import hw1; print(hw1.fizzbuzz(15))"
FizzBuzz

I could not simply insert a newline on Windows CMD, so this may be limited to Simple statements:

A simple statement is comprised within a single logical line. Several simple statements may occur on a single line separated by semicolons.

answered Dec 6, 2019 at 12:44

Comments

0

Method 1:

import hw1
hw1.fizzbuzz(15)

Method 2:

from hw1 import fizzbuzz
fizzbuzz(15)
answered Sep 14, 2014 at 19:21

Comments

0

I include this answer because this question is high in google in relevant search cases. Use case is executing a Python script to execute a function there with system arguments. Code example with currency exchange expense calculator

# 2000 euro 26.6.2017
# python countCurrencyRate.py 18870.75 18908.76
# Output: Currency exchange expense: 0.00201220185227
#
# For https://money.stackexchange.com/a/20277/1627
import sys
a1 = float( sys.argv[1] )
b1 = float( sys.argv[2] )
def countCurrencyRate(a=a1, b=b1):
 # a google currency rate, b destination currency rate; or reverse
 ave = (a+b)/2
 diff = abs(b-a)
 result = diff/ave
 print("Currency exchange expense: %s" % result)
if __name__ == "__main__":
 countCurrencyRate(a1, b1)

Python: 2.7.9

answered Jun 25, 2017 at 9:14

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.