What's new

Python HELP PYTHON

fuzzywubby

Forum Guru
Joined
Sep 20, 2018
Posts
4,587
Solutions
2
Reaction
1,865
Points
1,225
Gusto ko sanang gawin is maread yung product name and price. Pero yung nagagawa ko palang is iread yung product name lang sa loob ng file, hindi nasasama yung product price. Ano po kaya kulang sa code ko?

CODE:
Python:
def transact():  # DI BINABASA YUNG PRICE!!!!
    print("<Transact Sales>")
    temp = open("temp.txt", "x")
    inputs = []
    if inputs != '':
        while True:
            inp = input("Product Name (type 'x' to stop the transaction): ")
            if inp == 'x':
                temp.close()
                os.remove("temp.txt")
                break

            else:
                inputs.append("Product Name: " + inp)
                with open("products.txt", "r") as prod:
                    lines = prod.readlines()

                    with open("temp.txt", "a") as temp:
                        with open("LOGBOOK.txt", "a") as log:
                            for line in lines:
                                if str("Product Name: " + inp) in line:
                                    temp.write(line)
                                    log.write(line)

PRODUCT LIST (products.txt)
1649466406071.png


OUTPUT (not the required output):

1649466869994.png



 

Attachments

Last edited:
Ano po ba dapat ang tamang output nyo?
-----------------------------------------------
for line in lines:
if str("Product Name: " + inp) in line:
temp.write(line)
log.write(line)
-----------------------------------------------
string search yan pra sa "Product Name:" + input string in every line. Ano po ba ang gusto nyo makuha?
 
Last edited:
Ano po ba dapat ang tamang output nyo?
-----------------------------------------------
for line in lines:
if str("Product Name: " + inp) in line:
temp.write(line)
log.write(line)
-----------------------------------------------
string search yan pra sa "Product Name:" + input string in every line. Ano po ba ang gusto nyo makuha?
Sample output:

Product Name: Keyboard
Product Price: 100
 
I-include mo ang enumerate function, para makuha yung iteration number ng loop, tapos once na ma-trigger ang string search, iteration + 1 lang pra sa next line. capture the whole line, then i-append sa same file.
 
I-include mo ang enumerate function, para makuha yung iteration number ng loop, tapos once na ma-trigger ang string search, iteration + 1 lang pra sa next line. capture the whole line, then i-append sa same file.
Try ko po maya paps, maglinis lang ako bahay. Thanks

Yup ano bang expected output mo?
Parang ganto po, pag nilagay yung product name, next line naman yung price nya.

Sample output:

Product Name: Keyboard
Product Price: 100
 
Ask ko lang, yung product list ba manual mo lang yan?
May ginawa kasi ako baka makatulong pero JSON format and output txt file. Di ko sure kung ganito main idea mo :)

products.json
JSON:
[
    {
        "name": "Keyboard",
        "price": 100
    },
    {
        "name": "Mouse",
        "price": 200
    },
    {
        "name": "Monitor",
        "price": 600
    },
    {
        "name": "Headset",
        "price": 100.0
    }
]

Python:
import json
from pathlib import Path

file = Path('products.json')


def write_products():
    products = []
    print('Press ctrl + c to exit')

    while True:
        try:
            product = input("Product Name: ")
            price = int(input('Price: '))
            products.append({'name': product, 'price': price})
        except KeyboardInterrupt:
            print(f'\nProducts has been saved to: {file}')
            break
    with file.open('w') as w:
        json.dump(products, w, indent=4)


def transact():
    with file.open('r') as fp:
        products = json.load(fp)

    print('<Transact Sales>')

    logbook = Path('LOGBOOK.txt')

    products_output = []

    while True:
        inp = input('Product Name (type "x" to stop the transaction): ')
        if inp == 'x':
            break

        product = [p for p in products if p['name'] == inp]
        if not product:
            print(f'{product} is not available')
            continue
        product = product[0]

        name = product['name']
        price = product['price']
        products_output.append(f'Product Name: {name}\nProduct Price: {price}')

    logbook.write_text('\n\n'.join(product_output))
    print(f'Products output has been saved to: {logbook}')


transact()
# write_products()

OUTPUT
Code:
<Transact Sales>
Product Name (type "x" to stop the transaction): Keyboard
Product Name (type "x" to stop the transaction): Mouse
Product Name (type "x" to stop the transaction): Monitor
Product Name (type "x" to stop the transaction): Headset
Product Name (type "x" to stop the transaction): x
Products output has been saved to: LOGBOOK.txt


LOGBOOK.TXT output
Code:
Product Name: Keyboard
Product Price: 100

Product Name: Mouse
Product Price: 200

Product Name: Monitor
Product Price: 600

Product Name: Headset
Product Price: 100.0

Pero test mo muna suggestion nila para fix sa current codes mo :)
 
Last edited:
Ask ko lang, yung product list ba manual mo lang yan?
May ginawa kasi ako baka makatulong pero JSON format and output txt file. Di ko sure kung ganito main idea mo :)

products.json
JSON:
[
    {
        "name": "Keyboard",
        "price": 100
    },
    {
        "name": "Mouse",
        "price": 200
    },
    {
        "name": "Monitor",
        "price": 600
    },
    {
        "name": "Headset",
        "price": 100.0
    }
]

Python:
import json
from pathlib import Path

file = Path('products.json')


def write_products():
    products = []
    print('Press ctrl + c to exit')

    while True:
        try:
            product = input("Product Name: ")
            price = int(input('Price: '))
            products.append({'name': product, 'price': price})
        except KeyboardInterrupt:
            print(f'\nProducts has been saved to: {file}')
            break
    with file.open('w') as w:
        json.dump(products, w, indent=4)


def transact():
    with file.open('r') as fp:
        products = json.load(fp)

    print('<Transact Sales>')

    logbox = Path('LOGBOX.txt')

    products_output = []

    while True:
        inp = input('Product Name (type "x" to stop the transaction): ')
        if inp == 'x':
            break

        product = [p for p in products if p['name'] == inp]
        if not product:
            print(f'{product} is not available')
            continue
        product = product[0]

        name = product['name']
        price = product['price']
        products_output.append(f'Product Name: {name}\nProduct Price: {price}')

    logbox.write_text('\n\n'.join(product_output))
    print(f'Products output has been saved to: {logbox}')


transact()
# write_products()

OUTPUT
Code:
<Transact Sales>
Product Name (type "x" to stop the transaction): Keyboard
Product Name (type "x" to stop the transaction): Mouse
Product Name (type "x" to stop the transaction): Monitor
Product Name (type "x" to stop the transaction): Headset
Product Name (type "x" to stop the transaction): x
Products output has been saved to: LOGBOX.txt


LOGBOX.TXT output
Code:
Product Name: Keyboard
Product Price: 100

Product Name: Mouse
Product Price: 200

Product Name: Monitor
Product Price: 600

Product Name: Headset
Product Price: 100.0

Pero test mo muna suggestion nila para fix sa current codes mo :)
Thank you po sa idea paps. Itry ko rin po ito.
mag json format ka

mahirap mag raw parse
Yes sir, itong nasa taas gawin kong reference. Thank you sa respond.
 

Similar threads

Back
Top