硬币类硬件分配



所以我正在为类做一个Coin Flip class程序,而这段直接来自书中的代码在Pycharm和VSC中给了我错误。我已经重读了10遍,但找不到让程序运行的错误。试图弄清楚我是否遗漏了什么,或者示例源代码不正确


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()
``

您有一个等于零的变量self,您正试图访问它的一个名为"向上";

def __init__(self):
side.up = 'Heads'

最新更新