请代码查看我的示例 Python 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15623045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Please code review my sample Python program
提问by Kirk Rogers
I'm still learning Python as I want to teach the essential concepts of the language to eleven year old kids (I work as a teacher). We have done a bit of work in basic so they understand the essentials of programming and breaking down tasks into chunks and such like. Python is the language that is going to be taught all across the UK with the new curriculum coming in and I don't want to teach the kids bad habits. Below is a little program that I have written, yep I know it's bad but any advice about improvements would be very much appreciated.
我仍在学习 Python,因为我想向 11 岁的孩子教授语言的基本概念(我是一名教师)。我们在基础上做了一些工作,因此他们了解编程的要点,并将任务分解为块等。随着新课程的推出,Python 是将在英国各地教授的语言,我不想教孩子们坏习惯。下面是我写的一个小程序,是的,我知道它很糟糕,但任何关于改进的建议将不胜感激。
I am still plowing through tutorials on the language so please be gentle! :o)
我仍在努力学习有关语言的教程,所以请保持温和!:o)
# This sets the condition of x for later use
x=0
# This is the main part of the program
def numtest():
print ("I am the multiplication machine")
print ("I can do amazing things!")
c = input ("Give me a number, a really big number!")
c=float(c)
print ("The number", int(c), "multiplied by itself equals",(int(c*c)))
print("I am Python 3. I am really cool at maths!")
if (c*c)>10000:
print ("Wow, you really did give me a big number!")
else:
print ("The number you gave me was a bit small though. I like bigger stuff than that!")
# This is the part of the program that causes it to run over and over again.
while x==0:
numtest()
again=input("Run again? y/n >>>")
if x=="y":
print("")
numtest()
else:
print("Goodbye")
回答by John La Rooy
You don't seem to need the variable x
你似乎不需要变量 x
while True:
numtest()
again = input("Run again? y/n >>>")
if again == "y": # test "again", not "x"
print("")
else:
print("Goodbye")
break # This will exit the while loop
回答by Zokis
you have to break the loop
你必须打破循环
your while should be
你的时间应该是
while again == 'y':
而再次=='y':
thereby
从而
again = 'y'
def numtest():
print ("I am the multiplication machine")
print ("I can do amazing things!")
c = input("Give me a number, a really big number!")
c = float(c)
print ("The number", int(c), "multiplied by itself equals", (int(c * c)))
print("I am Python 3. I am really cool at maths!")
if (c * c) > 10000:
print ("Wow, you really did give me a big number!")
else:
print ("The number you gave me was a bit small though. I like bigger stuff than that!")
# This is the part of the program that causes it to run over and over again.
while again == 'y':
numtest()
again = input("Run again? y/n >>>")
if again != "y":
print("Goodbye")
回答by kojiro
Some hopefully useful commentary:
一些希望有用的评论:
Use a docstring instead of a comment to describe your function
使用文档字符串而不是注释来描述您的函数
def numtest():
"""Use a docstring. This is the main part of the program. Explain what a function is and why you want to use it. (Because it gives you scope and lets you simplify a complex set of procedures into a single operation.)"""
Use consistent style for your code and try to get your students to follow it, too.
为您的代码使用一致的风格,并尝试让您的学生也遵循它。
If you're not absolutely sure what style to follow, use PEP-8. (In your case there are differences in how you treat whitespace in the same operation on different lines.)
如果您不确定要遵循什么风格,请使用PEP-8。(在您的情况下,您在不同行上的相同操作中处理空格的方式有所不同。)
print ("The number", int(c), "multiplied by itself equals",(int(c*c)))
print("I am Python 3. I am really cool at maths!")
Why make a float here and an int later on?
为什么在这里做一个浮点数,然后再做一个整数?
It might be useful to teach how computers treat floating point operations differently from integer operations, but that's not really illustrated here.
教计算机如何以不同于整数运算的方式处理浮点运算可能很有用,但这里并未真正说明这一点。
c = float(c)
print("The number", int(c), "multiplied by itself equals", (int(c*c)))
You call numtesttwice in the loop
你numtest在循环中调用两次
Try this instead:
试试这个:
again = "y"
while again == "y":
numtest()
again = input("Run again? y/n >>>")
print("")
# Take this out of the loop.
print("Goodbye")
回答by cdarke
Since you wish to teach good style:
既然你想教好风格:
Don't use variable names like
xunless you are creating a graph. See PEP008 for naming conventions and style.Be consistent with your spaces:
c = input ("Give me a number, a really big number!") c=float(c)
x除非您正在创建图形,否则不要使用变量名称。有关命名约定和样式,请参阅 PEP008。与您的空间保持一致:
c = input ("Give me a number, a really big number!") c=float(c)
is not consistent. Which is better style?
不一致。哪种风格更好?
If you really want an infinite loop then:
如果你真的想要一个无限循环,那么:
while True:
numtest()
again = input("Run again? y/n >>>")
if again.lower().startswith("n"):
print("Goodbye")
break
Then again, some people think that using breakis bad style, do your agree? How would you rewrite the loop so break is not used? An exercise for your students maybe?
话说回来,有些人认为使用break是不好的风格,你同意吗?您将如何重写循环以便不使用中断?也许是为您的学生做的练习?

