7

我仍在学习 Python,因为我想向 11 岁的孩子(我是一名教师)教授该语言的基本概念。我们在基础方面做了一些工作,因此他们了解编程和将任务分解成块等的要点。随着新课程的推出,Python 是一种将在英国各地教授的语言,我不想教孩子们坏习惯。下面是我编写的一个小程序,是的,我知道这很糟糕,但任何关于改进的建议都将非常感激。

我仍在学习该语言的教程,所以请保持温柔!: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")
4

4 回答 4

10

你似乎不需要这个变量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
于 2013-03-25T19:31:29.483 回答
4

既然你想教好的风格:

  1. x除非您正在创建图形,否则不要使用变量名称。有关命名约定和样式,请参阅 PEP008。

  2. 与您的空间保持一致:

    c = input ("Give me a number, a really big number!")
    
    c=float(c)
    

不一致。哪种风格更好?

如果你真的想要一个无限循环,那么:

    而真:
        数测试()

        再次 = input("再次运行?是/否 >>>")

        如果再次.lower().startswith("n"):
            打印(“再见”)
            休息

再说一次,有些人认为使用break是不好的风格,你同意吗?您将如何重写循环以便不使用 break?给你的学生做个练习吧?

于 2013-03-25T19:39:29.507 回答
2

你必须打破循环

你的时间应该是

再次=='y':

从而

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")
于 2013-03-25T19:37:55.770 回答
1

一些希望有用的评论:

使用文档字符串而不是注释来描述您的函数

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.)"""

为您的代码使用一致的样式,并尝试让您的学生也遵循它。

如果您不确定要遵循哪种风格,请使用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!")

为什么在这里做一个浮点数,然后再做一个整数?

教授计算机如何以不同于整数运算的方式处理浮点运算可能很有用,但这里并没有真正说明。

c = float(c)
print("The number", int(c), "multiplied by itself equals", (int(c*c)))

numtest你在循环中调用了两次

试试这个:

again = "y"
while again == "y":
    numtest()
    again = input("Run again? y/n >>>")
    print("")

# Take this out of the loop.
print("Goodbye")
于 2013-03-25T19:39:11.890 回答