1
\$\begingroup\$

I have a very simple code that allows me to keep track of what the user inserts in a raw_input.

The kind of controls I check are if the user inserts "f" the program is finished or if the user inserts nothing, the program keeps on running remembering all the inputs the user has inserted.

It works but I think it's very ugly and rudimental.

I would love to get suggestions from experts so I can learn how to handle correctly simple tasks.

Here's the little code:

#!/usr/bin/python
import os
good_receipts = []
done = False
inserted_text = ''
while not done:
    # This function is os dependent and will work only if you use linux
    os.system('clear')
    inserted_text = raw_input("Insert receipt number (if you finished insert 'f'): ").upper()
    if inserted_text and inserted_text != 'F':
        good_receipts.append(inserted_text)
    while not inserted_text == 'F':
        # This function is os dependent and will work only if you use linux
        os.system('clear')
        if good_receipts:
            print "Here's your receipts:"
            print good_receipts
        if not good_receipts:
            print "You didn't enter a single receipt..."
        inserted_text = raw_input("Insert receipt number (if you finished insert 'f'): ").upper()
        if inserted_text and inserted_text != 'F':
            good_receipts.append(inserted_text)
    done = True
# This function is os dependent and will work only if you use linux
os.system('clear')
print "You did a great job, here's your receipts:"
print good_receipts
\$\endgroup\$
2
\$\begingroup\$

Here is what I came up with:

#!/usr/bin python
from os import system as sys

def GetReceipt():
    prompt = "Insert receipt number (if you finished insert 'f'): "
    inserted_text = ''
    while True:
        sys('clear')
        inserted_text = raw_input(prompt).upper()
        if inserted_text == 'F': break
        yield inserted_text


def final(receipt):
    sys('clear')
    print ["You did a great job, here's your receipts:\n"+' '.join(receipt), 
        "You did not enter a single receipt..."][len(receipt) == 0]

if __name__ == '__main__':
    valid_receipts = [ f for f in GetReceipt() ]
    final(valid_receipts)
\$\endgroup\$
1
  • \$\begingroup\$ list[bool] is a useful trick, but in this case I'd say it just makes the code harder to read. \$\endgroup\$
    – Changaco
    Sep 1 '13 at 12:54
1
\$\begingroup\$

I find it easier to reason about code that actually does something, so I made it display the sum of the inputted numbers.

#!/usr/bin/env python3

try:
    import readline
except ImportError:
    pass

numbers = []
print('Enter your numbers, one per line. Enter "f" when you\'re done.')

while True:
    try:
        inserted_text = input('> ')
    except EOFError:
        print()
        break
    except KeyboardInterrupt:
        print()
        continue
    if inserted_text.lower() == 'f':
        break
    if not inserted_text:
        continue
    try:
        n = int(inserted_text)
    except ValueError:
        print('That\'s not a valid number.')
        continue
    numbers.append(n)

if numbers:
    print('Here are your numbers and their sum:')
    print(' + '.join(map(str, numbers)), '=', sum(numbers))
else:
    print('You did not enter a single number...')

I wrote the code in python3 because there is no reason to be using python2 here.

Importing readline improves the usability of the input function on platforms that support it.

I kept the possibility of entering "f" to exit, but really most regular console users will just press Ctrl+D, which is handled by the except EOFError: clause.

I removed the calls to clear because they just annoy the user.

\$\endgroup\$
1
\$\begingroup\$

I would do:

#!/usr/bin/python
import os
good_receipts = []
inserted_text = 'go'
while inserted_text != 'F':
    # This function is os dependent and will work only if you use linux
    os.system('clear')
    inserted_text = raw_input("Insert receipt number "
                  "(if you finished insert 'f'): ").upper()
    if ''!=inserted_text!='F':
        good_receipts.append(inserted_text)
    if good_receipts:
        print "Here's your receipts:\n",good_receipts
    if not good_receipts:
        print "You didn't enter a single receipt..."
# This function is os dependent and will work only if you use linux
os.system('clear')
print "You did a great job, here's your receipts:\n",good_receipts
\$\endgroup\$
0

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.