Home > Mobile >  How to add a currency symbol to my array?
How to add a currency symbol to my array?

Time:11-09

Goal:

Display my list of commission pay with a £ symbol.

Explanation of what the problem is:

When it comes to displaying the result it does not print the whole result instead it just prints the £ symbol.

What I have tried:

As you can see in the code, I have attempted to convert the array into a string (putting it into its own variable) then I added the currency symbol (£) to it and asked for commission2 to be called. However, it does not show.

OUTPUTTED:

Name         Id           Houses Sold  Commission  
£1000

ki           2            2            £  

(the £1000 is just to show that the data is actually there, but for some reason when printing in the list, it is not displayed...)

OUTPUT DESIRED:

Name         Id           Houses Sold  Commission  

ki           2            2            £1000 

I've been at it for hours so any help would be greatly appreciated!

Code where the error occurs:

def print_entered_info(names, ids, num_sold_houses):
print()
row_width = 12
comission_per_house = 500
header = ['Name', 'Id', 'Houses Sold', 'Commission']
print(' '.join(f'{h:<{row_width}}' for h in header))
commission = [n * comission_per_house for n in num_sold_houses] 
commission2 = commission
commission2 = ''.join(str(e) for e in commission)
commission2= "£"   commission2
print(commission2)
for values in zip(*[names, ids, num_sold_houses, commission2]):
    print()
    print(' '.join(f'{v:<{row_width}}' for v in values))

My full code:

def get_int_input(prompt: str) -> int:
    num = -1
    while True:
        try:
            num = int(input(prompt))
            break
        except ValueError:
            print('Error: Enter an integer, try again...')
    return num

def get_yes_no_input(prompt: str) -> bool:
    allowed_responses = {'y', 'yes', 'n', 'no'}
    user_input = input(prompt).lower()
    while user_input not in allowed_responses:
        user_input = input(prompt).lower()
    return user_input[0] == 'y'

names = []
ids = []
num_sold_houses= []
def get_single_employee_info(names, ids, num_sold_houses):
 names.append(input('What is the employee\'s name?: '))
 ids.append(get_int_input('What is the employee\'s id?: '))
 num_sold_houses.append(get_int_input('How many houses did the employee sell?: '))

def get_houses_sold_info(names, ids, num_sold_houses):
    get_single_employee_info(names, ids, num_sold_houses)
    add_another_employee = get_yes_no_input('Add another employee [yes/no]?: ')
    while add_another_employee:
        get_single_employee_info(names, ids, num_sold_houses)
        add_another_employee = get_yes_no_input(
            'Add another employee [yes/no]?: ')

def print_entered_info(names, ids, num_sold_houses):
    print()
    row_width = 12
    comission_per_house = 500
    header = ['Name', 'Id', 'Houses Sold', 'Commission']
    print(' '.join(f'{h:<{row_width}}' for h in header))
    commission = [n * comission_per_house for n in num_sold_houses] 
    commission2 = commission
    commission2 = ''.join(str(e) for e in commission)
    commission2= "£"   commission2
    print(commission2)
    for values in zip(*[names, ids, num_sold_houses, commission2]):
        print()
        print(' '.join(f'{v:<{row_width}}' for v in values))
    print()   
    total_commission = sum(commission)
    print(f'Total Commission: £{total_commission}.00 (before bonus)')
    print()
    bonus = max(commission)
    if bonus >= max(commission):
     bonus = bonus*0.15
     bonus = (int(max(commission))   bonus)
     commission = bonus

    print("The person at the top of ranking gets: "   "£"   str(commission) "0") 
    print()
    rankings = sorted(zip(num_sold_houses, names), reverse=True)
    print('Ranking:')
    for houses_sold, name in rankings:
        print(f'{name} - {houses_sold}')

def main() -> None:
    print('Houses Sold Tracker')
    print('===================')
    names, ids, num_houses_sold = [], [], []
    get_houses_sold_info(names, ids, num_houses_sold)
    print_entered_info(names, ids, num_houses_sold)

if __name__ == '__main__':
    main() 

CodePudding user response:

Change out

commission2 = ''.join(str(e) for e in commission)

to

commission2 = ["£" str(e) for e in commission]

and remove the line under it. Your ''.join was taking 2 list elements and forcing them into a single string, which is not what you want for this logic. You want a list output for the commission2 variable so we create a list and append the stringified values to it along with the currency symbol.

Output:

Name         Id           Houses Sold  Commission  

Kevin        1            3            £1500       

Stacey       2            5            £2500       

Total Commission: £4000.00 (before bonus)

The person at the top of ranking gets: £2875.00

Ranking:
Stacey - 5
Kevin - 3
  • Related