首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >硬币类HW分配

硬币类HW分配
EN

Stack Overflow用户
提问于 2021-12-08 21:00:58
回答 1查看 65关注 0票数 0

因此,我正在为类做一个硬币翻转类程序,这个代码直接从书中给我在Pycharm和VSC中的错误。我已重读了这10次,无法找到错误,以使程序运行。试图找出我是否遗漏了什么,或者示例源代码已经关闭。

代码语言:javascript
运行
复制
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()
``
EN

回答 1

Stack Overflow用户

发布于 2022-03-02 17:33:45

您有一个等于零的变量,self,并且您正在尝试访问它的一个属性"up“。

代码语言:javascript
运行
复制
    def __init__(self):
        side.up = 'Heads'
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70281659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档