'Coin Class HW assignment
So I am doing a Coin Flip Class program for class, and this code straight from the book is giving me errors in Pycharm and VSC. I have reread this 10 times and can not find the error to get the program to run. Trying to figure out if I am missing something, or the example source code is off.
import random
# The Coin Class simulates a coin that can be flipped
class coin:
def __init__(self):
side.up = 'Heads'
# Side up data attribute w/ heads
# The Toss generates a random number in
# the range of 0 - 1. If the number is 0, then side up is heads, otherwise side up is tails
def toss(self):
if random.randint(0, 1) == 0:
self.sideup = 'Heads'
else:
self.sideup = 'Tails'
# The get_sideup method returns the value referenced by sideup
def get_sideup(self):
return self.sideup
# The main function
def main():
# create an object from the coin class
my_coin = coin()
# Display that side facing up
print('This side is up:', my_coin.get_sideup())
# Toss Coin
print('I am tossing the coin . . .')
my_coin.toss()
# Display the side of the coin that is facing up
print('This side is up:', my_coin.get_sideup())
# Call the main function
main()
``
Solution 1:[1]
You have a variable that is equal to nothing, self, and you are attempting to access an attribute of it called "up"
def __init__(self):
side.up = 'Heads'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | S.B |
