-6
\$\begingroup\$

Here is my latest version of my addition script. I have tried to take on all the advice I have been given by people like @Janos and @Hosch250 and also tried to keep to the guidelines set by the PEP8 (I recommend it to anyone who hasn't read it yet). Now I have done it, so please tell me if I have missed anything/there is a way to improve.

def main():
    pass


def space():
    print(" ")


if __name__ == '__main__':
    main()

    Previous1=0
    Repeat=True
    Numbers=[]
    space()
    print("Type 0 to end")
    while Repeat:
        AddNumber=float(input("Add a number: "))
        Numbers.append(AddNumber)
        if AddNumber == 0:
            Repeat=False
    for num in Numbers:
        print('{} + {} = '.format(num, Previous1))
        Previous1 += num
        space()
        print(Previous1)
        space()
        space()
    print('All your numbers add up to {}'.format(Previous1))
    input("/n Press ENTER to exit the program")
\$\endgroup\$
6
  • \$\begingroup\$ Also, I know I am going off on a tangent here, but is there a way to change the colour of the output text? \$\endgroup\$ – Joe Dec 12 '15 at 19:43
  • 7
    \$\begingroup\$ Did you actually read the PEP-8 guide? What did it tell you about variable namings? \$\endgroup\$ – Barry Dec 12 '15 at 19:49
  • \$\begingroup\$ You should use conventions \$\endgroup\$ – Joe Dec 12 '15 at 19:51
  • \$\begingroup\$ It is too late to edit it because it would invalidate the answer. \$\endgroup\$ – user34073 Dec 12 '15 at 19:55
  • \$\begingroup\$ You should add a link to the last question so people can compare the two and see were you where compared to now. \$\endgroup\$ – 13aal Dec 12 '15 at 20:08
10
\$\begingroup\$

First things first, PEP-8 recommends snake_case for variable namings. So definitely numbers, repeat, add_number, etc. Also Previous1 isn't a great name - what is the number doing there? Perhaps sum_so_far?

Getting all the Inputs

input() will raise EOFError when we run out of numbers, and the float() conversion will raise ValueError if the user enters not a float. Rather than having a sentinel value input (what if I want to do 1+2+0+4+5?), just use what the language already provides:

def input_numbers(prompt):
    numbers = []
    while True:
        try:
            numbers.append(float(input(prompt)))
        except ValueError:
            # maybe print an error
        except EOFError:
            break
    return numbers

main() and space()

What is main() doing? I could understand:

def main():
    # all the logic here

if __name__ == '__main__':
    main()

space() is useless.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.