Please code review my sample Python program [closed]

痴心易碎 提交于 2019-12-05 05:05:40

You don't seem to need the variable 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:

  1. Don't use variable names like x unless you are creating a graph. See PEP008 for naming conventions and style.

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

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

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 numtest twice in the loop

Try this instead:

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

# Take this out of the loop.
print("Goodbye")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!