Advertisement
jxsl13

python product input example

Apr 20th, 2019
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. #!/bin/env python3
  2.  
  3. if __name__ == "__main__":
  4.  
  5.     # a stands for append mode, meaning that everything written
  6.     # will be added to the end of the file
  7.     with open("products.txt", "a") as f:
  8.         # go to the end of the file.
  9.         # if the file is epmty, we are at the beginning of the file,
  10.         # otherwise we add lines to the end of the file.
  11.         # seek(offset[, whence])
  12.         # The default value for whence is SEEK_SET. Values for whence are:
  13.         #   SEEK_SET or 0 – start of the stream (the default); offset should be zero or positive
  14.         #   SEEK_CUR or 1 – current stream position; offset may be negative
  15.         #   SEEK_END or 2 – end of the stream; offset is usually negative
  16.         absolute_pos = f.seek(0, 2)
  17.  
  18.         if absolute_pos == 0:
  19.             # if end of file is actually the beginning of the file:
  20.             # add a header
  21.             # \t is a tab character, \n is a newline character indicating a linebreak
  22.             f.write("id\tname\tprice\n")
  23.  
  24.         while True:
  25.             try:
  26.                 # if the conversion from input string to int or float fails,
  27.                 # an exception is raised by python, that we catch
  28.                 product_id = int(input("Please enter the product id: "))
  29.                 product_name = input("Please enter the product name: ")
  30.  
  31.                 # in some regions a decimal comma is used instead of a decomal point
  32.                 product_price = input("Please enter the product price: ").replace(",", ".")
  33.                 product_price = float(product_price)
  34.             except Exception as e:
  35.                 # catches any exception raised
  36.                 #print(e) # you can print what kind of errors are raised if you want to see for yourself
  37.                 print("An error occurred, please enter the product again.")
  38.                 # goes back to the beginning of the loop and retries the input process
  39.  
  40.                 # here you could add a prompt, whether the user wants to actually continue
  41.                 # and if not, you break free from the while loop.
  42.                 continue
  43.  
  44.             line_to_append_to_file = f"{product_id}\t{product_name}\t{product_price}\n" # needs python >= 3.6
  45.             #line_to_append_to_file = "{id}\t{name}\t{price}\n".format(id=product_id,name=product_name, price=product_price) # works with python >= 3.0
  46.  
  47.             # write tab separated information to file
  48.             f.write(line_to_append_to_file)
  49.             print("Appending: ", line_to_append_to_file)
  50.  
  51.  
  52.             confimation = input("Do you want to enter another product?[Y/n]: ")
  53.             # removes whitespace and newline characters if there are any before or after the input string
  54.             confimation = confimation.strip()
  55.  
  56.             if confimation == "" or confimation.lower() == "y":
  57.                 # simply "enter"/"newline" or "Y"/"y" will allow to enter another item
  58.                 continue
  59.             else:
  60.                 # anything else will abort the while loop.
  61.                 break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement