Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在另一个函数中使用函数?python_Python_Oop - Fatal编程技术网

如何在另一个函数中使用函数?python

如何在另一个函数中使用函数?python,python,oop,Python,Oop,我正在努力做一场比赛,我真的被卡住了。问题是我不知道如何正确使用面向对象编程。程序应启动gameboard功能 每次当数字不等于arv时。它应返回带有一个“O”的新板 少一点 当您只需要一个对象实例时,使用面向对象编程往往会使程序过于复杂。我建议只有一个主要功能 尽管如此,我还是修改了你的程序。请尝试自己查找更改,因为我懒得解释它们,抱歉 from random import randint import time class Game(): def __init__(self

我正在努力做一场比赛,我真的被卡住了。问题是我不知道如何正确使用面向对象编程。程序应启动gameboard功能 每次当数字不等于arv时。它应返回带有一个“O”的新板 少一点


当您只需要一个对象实例时,使用面向对象编程往往会使程序过于复杂。我建议只有一个主要功能

尽管如此,我还是修改了你的程序。请尝试自己查找更改,因为我懒得解释它们,抱歉

from random import randint
import time

class Game():    
    def __init__(self):
        pass

    def beginning(self):
        print("How are you, and why are you playing my game?")
        bla = str(input())
        time.sleep(2)
        print("Hello," + bla + ", I am glad to see you!")
        time.sleep(2)
        print("Anyways, the you have to guess a number from 1-20")
        self.__board = ["O","O","O","O","O"]


    def gameboard(self):
        print(self.__board)
        self.__board.pop()
        return self.__board   

    def game(self):
        number = randint(1,20)
        print(number)
        x = 1
        while 5 > x:
            arv = input()
            self.arv__ = arv
            if arv == number:
                print("Lucky")
                break
            elif arv != number:
                print ("haha, try again")
                print("one life gone")
                self.gameboard()
                print(self.__board)
            x += 1

def Main():
    math = Game()
    math.beginning()
    math.game()


Main()
以下是您的程序版本,它避免了OO,并且更加简化:

from random import randint

lives = 5
print("Guess a number from 1 to 20")
number = randint(1, 20)
while (lives > 1 and number != int(input())):
    print("incorrect")
    print("lives: " + "O" * lives)
    lives -= 1

if lives == 0:
    print("The number was actually " + str(number))
else:
    print("You were right")

赛尔夫。还有多少生命?为什么你需要它成为一个列表?你的问题是什么?@BrenBarn请仔细阅读。。。“问题是我不知道如何正确使用面向对象编程”
from random import randint

lives = 5
print("Guess a number from 1 to 20")
number = randint(1, 20)
while (lives > 1 and number != int(input())):
    print("incorrect")
    print("lives: " + "O" * lives)
    lives -= 1

if lives == 0:
    print("The number was actually " + str(number))
else:
    print("You were right")