因此,我正在为类做一个硬币翻转类程序,这个代码直接从书中给我在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()
``
发布于 2022-03-02 17:33:45
您有一个等于零的变量,self,并且您正在尝试访问它的一个属性"up“。
def __init__(self):
side.up = 'Heads'
https://stackoverflow.com/questions/70281659
复制相似问题