Skip to content

Instantly share code, notes, and snippets.

@janosgyerik
Last active December 12, 2015 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janosgyerik/0d803a1a5409f50e350f to your computer and use it in GitHub Desktop.
Save janosgyerik/0d803a1a5409f50e350f to your computer and use it in GitHub Desktop.

Code review for: http://codereview.stackexchange.com/questions/113743/number-addition#113743

Use booleans

Why do you use the strings "True" and "False" here?

repeat = "True"
numbers = []
print("Type 00 to end")
space()
while repeat == "True":
    addnumber = int(input("Add a number: "))
    numbers.append(addnumber)
    tna += 1
    if addnumber == 00:
        repeat = "False"

Use proper booleans instead:

repeat = True
numbers = []
print("Type 00 to end")
space()
while repeat:
    addnumber = int(input("Add a number: "))
    numbers.append(addnumber)
    tna += 1
    if addnumber == 00:
        repeat = False

Misleading 00

You know that 00 is the same as 0? The message to the user says to "Type 00 to end", but it will end if he types 0 too. As you can verify yourself in a Python interpreter:

>>> int('00')
0

So the message is misleading, the code comparing to 00 is strange, and should be fixed.

tna

Instead of this line with its comment:

for i in range(tna):  # tna = total numbers added

It would have been better to just spell it out, and use a variable name that is understandable without further explanation.

But in this particular example, you didn't need that variable at all, you could iterate over the numbers list directly:

for num in numbers:
    print('{} + {} = '.format(num, previous))
    previous += num
    space()
    print(previous)
    space()
    space()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment