我好吗? [英] How am I doing?

查看:47
本文介绍了我好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请不要笑,这是我的第一个Python脚本,我没有

查看手册寻求帮助......


导入字符串

随机导入


类hiScores:


hiScores = [''10000Alpha'',' '07500Beta'',''05000Gamma'',''02500Delta'','00000Epsilon'']

def showScores(个体经营):

for在self.hiScores中输入:

print entry [0:5]," - ,条目[5:]


def addScore(自我,得分,姓名):

newScore = string.zfill(得分,5)

self.hiScores.append(newScore + name)

self.hiScores.sort(reverse = True)


if len( self.hiScores)== 6:

del self.hiScores [-1]


a = hiScores()

print" ;原始分数\ n ---------------"

a.showScores()


而1 :

newScore = random.randint(0,10000)

if string.zfill(newScore,5)> a.hiScores [4] [0:5]:

打印祝贺你,得分%d %newScore

name = raw_input(" Please enter your name:")

a.addScore(newScore,name)

a。 showScores()

继续


我可以做的任何不同的事情或任何坏习惯你认为我有可能导致最终的厄运我真的很感激。


TIA

解决方案

" Jason" < JA *** @ jasonmhirst.co.uk>写道:

请不要笑,这是我的第一个Python脚本,我没有看过手册寻求帮助......


迟早你应该;)

导入字符串


不需要它现代蟒蛇;改为使用字符串方法。

随机导入

类hiScores:


常见的约定是对类使用大写单词,例如

HiScores。

hiScores = [''10000Alpha'',''07500Beta'',''05000Gamma'',''02500Delta'','00000Epsilon'' ]


hiScores最好在制作实例时作为参数给出,

不作为类实例硬编码。此外,最好将

分数与名称分开。那么hiScores可以是一个(分数,名称)

元组的列表,例如[('''10000'',''Alpha''),('''07500'',''Beta''),...,(''00000'',

'' Epsilon'')]:


def __init __(self,hiScores):

self.hiScores = [(条目[:5],条目[5: ])进入hiScores]

def showScores(self):
用于输入self.hiScores:
print entry [0:5]," - ",entry [5:]


如果你将得分与构造函数中的名字分开,你就不会b / b
必须拆分每次showScores被调用的条目:

def showScores(self):

得分,姓名在self.hiScores:

打印得分," ; - ",名称


您可以使用字符串插值更紧凑地表达:

def showScores(self):

进入self.hiScores:

print"%s - %s" %entry

def addScore(self,score,name):
newScore = string.zfill(得分,5)
self.hiScores.append(newScore + name)
self.hiScores.sort(reverse = True)


如果一次添加一个条目,保持列表更有效

已排序并使用bisect.insort函数代替整个

列表:


bisect.insort(self.hiScores,(newScore,name))

如果len(self.hiScores)== 6:


使用你给出的hiScores,这个测试是没用的;你开始使用5

条目并添加一个,所以你知道现在有6个。在更一般的

情况下,在构造函数中对初始hiScores进行排序,并获取前5个
条目。

del self.hiScores [ - 1]


你也可以通过self.hiScores.pop删除列表的最后一个元素()

a = hiScores()
print" ;原始分数\ n ---------------"
a.showScores()

而1:
newScore = random .randint(0,10000)
如果string.zfill(newScore,5)> a.hiScores [4] [0:5]:


两件事:

- string.zfill(newScore,5)更好地写成newScore.zfill(5)

- a.hiScores [4] [0:5]是神秘的;最好写一个方法给你

最后一个分数,这样你就可以拼写为a.lastScore():


def lastScore(self ):

返回a.hiScores [-1] [0]#假设(得分,姓名)条目

打印恭喜,你得分%d %newScore
name = raw_input(" Please enter your name:")
a.addScore(newScore,name)
a.showScores()
继续


继续在你说的那条线上做什么都没做。你什么时候想要

你的程序退出?事实上,它将永远循环。

任何我可以做的不同或任何坏习惯你觉得我有可能导致最终的厄运我真的很感激。

TIA




HTH,
George


George Sakkis写道:

" Jason" < JA *** @ jasonmhirst.co.uk>写道:

请不要笑,这是我的第一个Python脚本,我没有看过手册寻求帮助......



迟早你应该;)

import string



不需要它现代python ;改为使用字符串方法。

随机导入

类hiScores:



常见的约定是使用大写单词对于课程,例如
HiScores。

hiScores = [''10000Alpha'',''07500Beta'',''05000Gamma'',''02500Delta'',' '00000Epsilon'']



在制作实例时,最好将hiScores作为参数给出,而不是硬编码为类实例。此外,最好将
分数与名称分开。那么hiScores可以是一个(分数,名称)元组的列表,例如, [('''10000'',''Alpha''),('''07500'',''Beta''),...,(''00000'',
''Epsilon'') ]:

def __init __(self,hiScores):
self.hiScores = [(条目[:5],条目[5:])进入hiScores]
< blockquote class =post_quotes> def showScores(self):
用于输入self.hiScores:
print entry [0:5]," - ",entry [5:]



如果你将得分与构造函数中的名称分开,那么每次showScores都不必分割条目被称为:
def showScores(self):
得分,姓名在self.hiScores:
打印得分," - ,,名称

你可以使用字符串插值更紧凑地表达:
def showScores(self):
用于输入self.hiScores:
print %s - %s %entry

def addScore(self,score,name):
newScore = string.zfill(得分,5)
self.hiScores.append(newScore +姓名)
self.hiScores.sort(reverse = True)



如果一次添加一个条目,保持列表排序更有效率
并使用bisect.insort函数而不是整理整个
列表:

bisect.insort(self.hiScores,(newScore,name))

如果len(self.hiScores)== 6:



使用你给出的hiScores,这个测试是没用的;你开始使用5
条目并添加一个,所以你知道现在有6个。在更一般的情况下,在构造函数中对初始hiScores进行排序并获取前5个条目。

del self.hiScores [-1]



你也可以通过self.hiScores.pop删除列表的最后一个元素()

a = hiScores()
打印原创成绩\ n ---------------"
a.showScores()

而1:
newScore = random.randint(0,10000)
如果string.zfill(newScore,5)> a.hiScores [4] [0:5]:



两件事:
- string.zfill(newScore,5)更好地写成newScore.zfill(5)
- a.hiScores [4] [0:5]是神秘的;最好写一个方法给你最后一个分数,这样你就可以拼写为a.lastScore():

def lastScore(self):
返回一个.hiScores [-1] [0]#assume(score,name)条目

print恭喜,你得分%d %newScore
name = raw_input(" Please enter your name:")
a.addScore(newScore,name)
a.showScores()
继续



继续在你说的那条线上做什么都没做。您想什么时候退出程序?事实上,它将永远循环。

我可以做的任何不同的事情或任何坏习惯。你认为我有可能导致最终的厄运我真的很感激。

TIA



HTH,
George



LOL - 亲爱的!


我当然可以看到很多更好的方法,你已经指出了

乔治。首先,我想要去元组或

字典模式,但是你不能直接对它们进行排序

。根据我的理解,你不可能,但是你所展示的例子显示出明显有意义并且更加实用。


从来不知道Pop命令!!


再次感谢您的帮助,非常感谢。


Jason< ja *** @ jasonmhirst.co.uk>写道:

请不要笑,这是我的第一个Python脚本,我没有看过手册寻求帮助......

导入字符串
导入随机

类hiScores:
hiScores = [''10000Alpha'',''07500Beta'',''05000Gamma'','' 02500Delta'','00000Epsilon'']

def showScores(self):
用于输入self.hiScores:
print entry [0:5]," - ,条目[5:]

def addScore(自我,得分,姓名):
newScore = string.zfill(得分,5)
self.hiScores.append (newScore + name)
self.hiScores.sort(reverse = True)

如果len(self.hiScores)== 6:
del self.hiScores [-1]

a = hiScores()
print" Original Scores\\\
---------------"
a.showScores( )

1:
newScore = random.randint(0,10000)
如果string.zfill(newScore,5)> a.hiScores [4] [0: 5]:
打印祝贺你,得分%d %newScore
name = raw_input(" Please enter your name:")
a.addScore(newScore,name)
a.showScores()
继续

我可以做的任何不同的事情或任何坏习惯你认为我有可能导致最终的厄运我真的很感激。




乔治已经涵盖了很多东西。我想做一般性的评论:


标准的习惯用法是将脚本的所有可执行代码放在

函数中,(比如说main ;),然后调用该函数iff你的代码运行

作为脚本:


def main():

a = hiScores()

...


if __name__ ==" __ main __":

main()


这样你的程序可以被其他应用程序用作模块,

允许你的类/函数/等。被其他

申请重复使用而无需将其复制出您的计划。


< mike

-

Mike Meyer< mw*@mired.org> http://www.mired.org/home/mwm/

独立的WWW / Perforce / FreeBSD / Unix顾问,电子邮件以获取更多信息。


Please don''t laugh, this is my FIRST Python script where I haven''t
looked at the manual for help...

import string
import random

class hiScores:

hiScores=[''10000Alpha'',''07500Beta'',''05000Gamma'',''02500Delta'' ,''00000Epsilon'']

def showScores(self):
for entry in self.hiScores:
print entry[0:5]," - ",entry[5:]

def addScore(self,score,name):
newScore=string.zfill(score,5)
self.hiScores.append(newScore+name)
self.hiScores.sort(reverse=True)

if len(self.hiScores)==6:
del self.hiScores[-1]

a=hiScores()
print "Original Scores\n---------------"
a.showScores()

while 1:
newScore=random.randint(0,10000)
if string.zfill(newScore,5)>a.hiScores[4][0:5]:
print "Congratulations, you scored %d " % newScore
name=raw_input("Please enter your name :")
a.addScore(newScore,name)
a.showScores()
continue

Anything I could have done differently or any "bad-habits" you think I
have which could lead to ultimate doom I really appreciate to know.

TIA

解决方案

"Jason" <ja***@jasonmhirst.co.uk> wrote:

Please don''t laugh, this is my FIRST Python script where I haven''t
looked at the manual for help...
Sooner or later you should ;)
import string
Don''t need it it modern python; use string methods instead.
import random

class hiScores:
The common convention is to use Capitalized words for classes, e.g.
HiScores.
hiScores=[''10000Alpha'',''07500Beta'',''05000Gamma'',''02500Delta'' ,''00000Epsilon'']
hiScores should better be given as parameter when an instance is made,
not hardcoded as a class instance. Also, it is better to separate the
score from the name. Then hiScores can be, say, a list of (score,name)
tuples, e.g. [(''10000'', ''Alpha''), (''07500'', ''Beta''), ..., (''00000'',
''Epsilon'')]:

def __init__(self, hiScores):
self.hiScores = [(entry[:5], entry[5:]) for entry in hiScores]
def showScores(self):
for entry in self.hiScores:
print entry[0:5]," - ",entry[5:]
If you separate the score from the name in the constructor, you don''t
have to split the entries every time showScores is called:
def showScores(self):
for score,name in self.hiScores:
print score, " - ", name

You can express the same more compactly using string interpolation:
def showScores(self):
for entry in self.hiScores:
print "%s - %s" % entry
def addScore(self,score,name):
newScore=string.zfill(score,5)
self.hiScores.append(newScore+name)
self.hiScores.sort(reverse=True)
If you add one entry at a time, it is more efficient to keep the list
sorted and use the bisect.insort function instead of sorting the whole
list:

bisect.insort(self.hiScores, (newScore,name))
if len(self.hiScores)==6:
With your given hiScores, this test is useless; you started with 5
entries and added one so you know there are 6 now. In the more general
case, sort the initial hiScores in the constructor and take the top 5
entries.
del self.hiScores[-1]
You can also remove the last element of a list by self.hiScores.pop()
a=hiScores()
print "Original Scores\n---------------"
a.showScores()

while 1:
newScore=random.randint(0,10000)
if string.zfill(newScore,5)>a.hiScores[4][0:5]:
Two things:
- string.zfill(newScore,5) is better written as newScore.zfill(5)
- a.hiScores[4][0:5] is cryptic; it is better to write a method to give
you the last score so that you can spell it as a.lastScore():

def lastScore(self):
return a.hiScores[-1][0] # assuming (score,name) entries
print "Congratulations, you scored %d " % newScore
name=raw_input("Please enter your name :")
a.addScore(newScore,name)
a.showScores()
continue
"continue" doesn''t do anything at the line you put it. When do you want
your program to exit ? As it is, it will loop forever.
Anything I could have done differently or any "bad-habits" you think I
have which could lead to ultimate doom I really appreciate to know.

TIA



HTH,
George


George Sakkis wrote:

"Jason" <ja***@jasonmhirst.co.uk> wrote:

Please don''t laugh, this is my FIRST Python script where I haven''t
looked at the manual for help...



Sooner or later you should ;)

import string



Don''t need it it modern python; use string methods instead.

import random

class hiScores:



The common convention is to use Capitalized words for classes, e.g.
HiScores.

hiScores=[''10000Alpha'',''07500Beta'',''05000Gamma'',''02500Delta'' ,''00000Epsilon'']



hiScores should better be given as parameter when an instance is made,
not hardcoded as a class instance. Also, it is better to separate the
score from the name. Then hiScores can be, say, a list of (score,name)
tuples, e.g. [(''10000'', ''Alpha''), (''07500'', ''Beta''), ..., (''00000'',
''Epsilon'')]:

def __init__(self, hiScores):
self.hiScores = [(entry[:5], entry[5:]) for entry in hiScores]

def showScores(self):
for entry in self.hiScores:
print entry[0:5]," - ",entry[5:]



If you separate the score from the name in the constructor, you don''t
have to split the entries every time showScores is called:
def showScores(self):
for score,name in self.hiScores:
print score, " - ", name

You can express the same more compactly using string interpolation:
def showScores(self):
for entry in self.hiScores:
print "%s - %s" % entry

def addScore(self,score,name):
newScore=string.zfill(score,5)
self.hiScores.append(newScore+name)
self.hiScores.sort(reverse=True)



If you add one entry at a time, it is more efficient to keep the list
sorted and use the bisect.insort function instead of sorting the whole
list:

bisect.insort(self.hiScores, (newScore,name))

if len(self.hiScores)==6:



With your given hiScores, this test is useless; you started with 5
entries and added one so you know there are 6 now. In the more general
case, sort the initial hiScores in the constructor and take the top 5
entries.

del self.hiScores[-1]



You can also remove the last element of a list by self.hiScores.pop()

a=hiScores()
print "Original Scores\n---------------"
a.showScores()

while 1:
newScore=random.randint(0,10000)
if string.zfill(newScore,5)>a.hiScores[4][0:5]:



Two things:
- string.zfill(newScore,5) is better written as newScore.zfill(5)
- a.hiScores[4][0:5] is cryptic; it is better to write a method to give
you the last score so that you can spell it as a.lastScore():

def lastScore(self):
return a.hiScores[-1][0] # assuming (score,name) entries

print "Congratulations, you scored %d " % newScore
name=raw_input("Please enter your name :")
a.addScore(newScore,name)
a.showScores()
continue



"continue" doesn''t do anything at the line you put it. When do you want
your program to exit ? As it is, it will loop forever.

Anything I could have done differently or any "bad-habits" you think I
have which could lead to ultimate doom I really appreciate to know.

TIA



HTH,
George


LOL - O dear!!

Well I can certainly see a lot of better methods that you''ve pointed out
George. For one thing, I was ''wanting'' to go down the tuples or
dictionary mode but was under the impression you couldn''t sort them
directly. From what I understand, you can''t, but the example you have
shown clearly makes sense and a hell-of-alot more practical.

Never knew about the Pop command!!

Thanks again for the help though, really appreciate it.


Jason <ja***@jasonmhirst.co.uk> writes:

Please don''t laugh, this is my FIRST Python script where I haven''t
looked at the manual for help...

import string
import random

class hiScores:
hiScores=[''10000Alpha'',''07500Beta'',''05000Gamma'',''02500Delta'' ,''00000Epsilon'']

def showScores(self):
for entry in self.hiScores:
print entry[0:5]," - ",entry[5:]

def addScore(self,score,name):
newScore=string.zfill(score,5)
self.hiScores.append(newScore+name)
self.hiScores.sort(reverse=True)

if len(self.hiScores)==6:
del self.hiScores[-1]

a=hiScores()
print "Original Scores\n---------------"
a.showScores()

while 1:
newScore=random.randint(0,10000)
if string.zfill(newScore,5)>a.hiScores[4][0:5]:
print "Congratulations, you scored %d " % newScore
name=raw_input("Please enter your name :")
a.addScore(newScore,name)
a.showScores()
continue

Anything I could have done differently or any "bad-habits" you think I
have which could lead to ultimate doom I really appreciate to know.



George already covered a lot of things. I wanted to make a general comment:

The standard idiom is to put all the executable code for a script in a
function, (say "main"), then invoke that function iff your code is run
as a script:

def main():
a = hiScores()
...

if __name__ == "__main__":
main()

That way your program can be used as a module by other applications,
allowing your classes/functions/etc. to be reused by other
applications without having to copy them out of your program.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.


这篇关于我好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆