Guys can you help please? You see I created a module (if you want to know how get Hello world book 2nd edition) and imported it but the functions don't work. Here are the module and scripts
class Coin:
def flip_coin(self, op):
a = random.choice(op)
easygui.msgbox(a)
import random
import easygui
from coin import Coin
op = ["Heads", "Tails"]
easygui.msgbox("Ready?")
Coin.flip_coin()
You guys wanted to see the error:
Traceback (most recent call last): File "C:\Python27\Hello world\coin", line 9, in <module> flip_coin() TypeError: flip_coin() takes exactly 2 arguments (0 given)
The problem is (aside from the fact that your question doesn't have the actual error ;-) ) that you haven't instantiated an object of your Coin
class, and you don't pass in op
.
In addition, since Coin
lives in a separate module, it too needs to have random
and easygui
imported if you use them.
import random
import easygui
class Coin:
def flip_coin(self, op):
a = random.choice(op)
easygui.msgbox(a)
import easygui
from coin import Coin
coin = Coin() # instantiate the class
easygui.msgbox("Ready?")
coin.flip_coin(["Heads", "Tails"])
However, it's not Pythonic to use a class when you don't actually need to store state.
Instead, you can just leave def flip_coin(op):
a free function -- and also, since coins don't usually have more options than heads or tails:
import random
import easygui
def flip_coin():
a = random.choice(["Heads", "Tails"])
easygui.msgbox(a)
easygui.msgbox("Ready?")
flip_coin()