请编写一下我的示例Python程序

时间:2013-03-25 19:25:02

标签: python loops while-loop

我还在学习Python,因为我想向十一岁的孩子讲授这门语言的基本概念(我是一名教师)。我们在基础方面做了一些工作,因此他们了解编程的基本要素,并将任务分解成块等。 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 个答案:

答案 0 :(得分: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

答案 1 :(得分:4)

既然你想教好风格:

  1. 除非您要创建图表,否则请勿使用x之类的变量名称。有关命名约定和样式,请参阅PEP008。

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

    c = input ("Give me a number, a really big number!")
    
    c=float(c)
    
  3. 不一致。哪种风格更好?

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

        while True:
            numtest()
    
            again = input("Run again? y/n >>>")
    
            if again.lower().startswith("n"):
                print("Goodbye")
                break
    

    然后,有些人认为使用break是不好的风格,你同意吗?你怎么重写循环所以不使用break?为学生练习可能吗?

答案 2 :(得分: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")

答案 3 :(得分:1)

一些有希望有用的评论:

使用docstring而不是注释来描述您的函数

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

为什么要在这里制作一个浮点数并在以后制作一个int?

教授计算机如何处理浮点运算与整数运算不同,这可能是有用的,但这里没有说明。

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