Please code review my sample Python program

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.

I am still plowing through tutorials on the language so please be gentle! :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")

你似乎不需要变量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

Since you wish to teach good style:

  • Don't use variable names like x unless 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)
    
  • 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 break is bad style, do your agree? How would you rewrite the loop so break is not used? An exercise for your students maybe?


    you have to break the loop

    your while should be

    while again == '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")
    
    链接地址: http://www.djcxy.com/p/54306.html

    上一篇: Python类方法的一个示例用例是什么?

    下一篇: 请仔细阅读我的示例Python程序