python:使用类来跟踪另一个类使用的数据(python: using a class to keep track of data used by another class)

编程入门 行业动态 更新时间:2024-10-27 08:38:21
python:使用类来跟踪另一个类使用的数据(python: using a class to keep track of data used by another class)

我正在通过书籍和互联网学习Python。 我试图在一个单独的课程中保持游戏的分数。 为了测试我的想法,我构建了一个简单的例子。 由于某种原因,它看起来太复杂了。 是否有更简单/更好/更Pythonic的方式来做到这一点?

我的代码如下:

import os class FOO(): def __init__(self): pass def account(self, begin, change): end = float(begin) + float(change) return (change, end) class GAME(): def __init_(self): pass def play(self, end, game_start): os.system("clear") self.foo = FOO() print "What is the delta?" change = raw_input('> ') if game_start == 0: print "What is the start?" begin = raw_input('> ') else: begin = end change, end = self.foo.account(begin, change) print "change = %r" % change print "end = %r" % end print "Hit enter to continue." raw_input('> ') self.play_again(end, game_start) def play_again(self, end, game_start): print "Would you like to play again?" a = raw_input('> ') if a == 'yes': game_start = 1 self.play(end, game_start) else: print "no" exit(0) game = GAME() game.play(0, 0)

I'm learning Python via book and internet. I'm trying to keep score of a game in a separate class. In order to test my idea, i've constructed a simple example. It looks too complicated for some reason. Is there a simpler/better/more Pythonic way to do this?

My code is as follows:

import os class FOO(): def __init__(self): pass def account(self, begin, change): end = float(begin) + float(change) return (change, end) class GAME(): def __init_(self): pass def play(self, end, game_start): os.system("clear") self.foo = FOO() print "What is the delta?" change = raw_input('> ') if game_start == 0: print "What is the start?" begin = raw_input('> ') else: begin = end change, end = self.foo.account(begin, change) print "change = %r" % change print "end = %r" % end print "Hit enter to continue." raw_input('> ') self.play_again(end, game_start) def play_again(self, end, game_start): print "Would you like to play again?" a = raw_input('> ') if a == 'yes': game_start = 1 self.play(end, game_start) else: print "no" exit(0) game = GAME() game.play(0, 0)

最满意答案

以下是我将如何格式化代码:

import os class Game(object): def play(self, end, game_start=None): os.system("clear") change = input('What is the delta? ') # Shorthand for begin = game_start if game_start else end begin = game_start or end end = float(begin + change) print "change = {}".format(change) print "end = {}".format(end) self.play_again(end, game_start) def play_again(self, end, game_start): raw_input('Hit enter to continue.') if raw_input('Would you like to play again? ').lower() in ['yes', 'y']: self.play(end, game_start) else: exit(0) if __name__ == '__main__': game = Game() game.play(0, 0)

还有一些提示:

我不会创建一个只包含执行一个特定任务的代码的新类。 如果类不接受参数或不简化代码,请不要创建它。 但是,您的Game类是一个例外,因为您可能会添加更多代码。 在Python中,类是用CamelCase编写的。 全局常量通常以UPPERCASE形式编写。 raw_input()返回一个字符串。 input()返回计算到Python对象的字符串。

I asked the question a better way and got what I was looking for here:

python: how do I call a function without changing an argument?

更多推荐

本文发布于:2023-08-04 15:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417741.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   python   class   data   track

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!