Please code review my sample Python program

53,661

Solution 1

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

Solution 2

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?

Solution 3

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

Solution 4

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")
Share:
53,661

Related videos on Youtube

Kirk Rogers
Author by

Kirk Rogers

Updated on July 09, 2022

Comments

  • Kirk Rogers
    Kirk Rogers almost 2 years

    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")
    
    • mgilson
      mgilson about 11 years
      What's your question exactly? If you're looking for a code review, codereview.stackexchange would probably be a better venue.
    • Waleed Khan
      Waleed Khan about 11 years
      Avoid putting a space between a function call and its following parenthesis. You might (not sure) have some ambiguity with Python 2 because print ("string", 10) would be printing a tuple but print("string", 10) would be printing two values.
  • DSM
    DSM about 11 years
    As written, this will call numtest() twice in succession without asking in between if you want to run it again. I don't think the first if branch adds anything.
  • John La Rooy
    John La Rooy about 11 years
    @DSM good spotting. It just adds a blank line now
  • kojiro
    kojiro about 11 years
    +1 because you trailed a question after all your comments, thus keeping to the pedagogical point.
  • Kirk Rogers
    Kirk Rogers about 11 years
    Thank you. In a few lines you have helped me out with the while true command and introduced me to the command 'break' thanks.
  • Kirk Rogers
    Kirk Rogers about 11 years
    Thank you for this. It really helped a lot. Very clear and makes sense.
  • Kirk Rogers
    Kirk Rogers about 11 years
    Thanks for your help. I think I posted this in the wrong section and should have used code review, whoops. I have planned some stuff on how python works with numbers but I know they are going to get really confused between float and integers, they are only 9-11 years old. Still, they picked up basic well enough at the age of seven and eight so time to give them a push! Thanks again.
  • kojiro
    kojiro about 11 years
    To be sure, @KirkRogers, it's not how just Python works with numbers. It's how floating point arithmetic works, and very few programming languages don't work that way. It would be hard to give a really good treatment of that without teaching some binary math, though.
  • Kirk Rogers
    Kirk Rogers about 11 years
    Thanks for your advice. we have done some work with binary mathematics though probably nowhere at the level you are thinking of. The biggest problem I am going to face is how Python deals with numbers.
  • Kirk Rogers
    Kirk Rogers about 11 years
    For example, In BASIC INT means just knock off everything after the decimal point but in Python a variable can be defined as int. The float of the number will always be x.o
  • Kirk Rogers
    Kirk Rogers about 11 years
    I'll give you an example; when we write a program in basic to check the divisibility of a number we compared the integer of a division by the original answer of the division. If the two were the same, then that number was a multiple of x. . Ie. 10/2 is the same as the integer of 10/2 so ten is a multiple of 2. In python though, this language is totally out the window. The number has to be converted to a float and the when you compare the float to the integer you are comparing 5.0 to 5 Obviously not the same. How do you do it? I am almost ripping my hair out here. :o) Thank you & Regards Kirk
  • kojiro
    kojiro about 11 years
    Well, that's a different question, but if you want to check the divisibility of a number you should use the modulo operator.
  • Kirk Rogers
    Kirk Rogers about 11 years
    Cheers for that, I was thinking that floor might be the way to go, you've just confirmed it.
  • cdarke
    cdarke about 11 years
    @KirkRogers: Since your students are "only" nine years old then you have an even greater responsibility to show readable, clear, code. If you show an impenetrable mess then you might put them off or make them think that sloppy is OK. Lessons learn at that age stay with you the rest of your life, and are difficult to break later.
  • kojiro
    kojiro about 11 years
    @KirkRogers what? But I didn't say anything about floor! And it's not the way to go. Use the modulo operator.
  • Kirk Rogers
    Kirk Rogers about 11 years
    Sorry, my mistake! getting confused between the two terms. Mod as in if the mod ever flags as zero it's not prime.