Jun-30-2018, 11:11 AM
(This post was last modified: Jun-30-2018, 11:21 AM by python_alex.)
Hello, I am trying to learn how to create a class by following the toss coin exercise from a textbook but I kept getting the error (NameError: name Coin is not defined). I copied exactly as shown in the texbook and I am using the latest IDLE 3.65. Wonder if anyone could advise me what I did wrong or is it a version compatibility problem. Thanks.
Noted, thanks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import random class Coin: def __init__( self ): self .sideup = 'Heads' def toss( self ): if random.ranint( 0 , 1 ) = = 0 : self .sideup = 'Heads' else : self .sideup = 'Tails' def get_sideup( self ): return self .sideup def main(): mycoin = Coin() print ( 'This side is up: ' , my_coin.get_sideup()) print ( 'I am tossing the coin...' ) my_coin.toss() print ( 'This side is up: ' , my_coin.get_sideup()) main() |
Noted, thanks.