Harry Kim - 3 months ago 13

Python Question

I have been working on the program that lists out the products (with their cost and quantity) , and they are separately stored in 3 different lists.

`while valid ==1 :`

if user_choice == 's':

user_product = str(input("Enter a product name: "))

valid = 2

elif user_choice == 'l':

print ("Product" + " " + "Quantity" +" "+ "Cost")

c = 0

while c < len(product_names):

print (product_names[c] + " " + str(product_costs[c]) + " "+ str(quantity[c]))

c +=1

valid = 0

break

valid = 0

So basically I am not sure on how to actually make output on line 6 and

line 9 be aligned together because I'll be getting a disorganized output because the product names differ in length, cost and quantity differ in length too.

Can anybody teach me how to actually align them all properly so that they might

look like a table?

Thanks so much!

Answer

Here is what you wanted, *exactly by the prescribed order*.

```
n = -1 # Intentionally an incorrect value
# Ask user for the number while he/she doesn't enter a correct one
while n < 10:
n = int(input("Enter an integer number greater or equal 10: "))
# Preparation for Sieve of Eratosthenes
flags_list = ["P"] # 1st value
flags_list = flags_list * (n + 1) # (n + 1) values
flags_list[0] = "N" # 0 is not a prime number
flags_list[1] = "N" # 1 is not a prime number, too
# Executing Sieve of Eratosthenes
for i in range(2, n + 1):
if flags_list[i] == "P":
for j in range(2 * i, n + 1, i):
flags_list[j] = "N"
# Creating the list of primes from the flags_list
primes = [] # Empty list for adding primes into it
for i in range(0, n + 1):
if flags_list[i] == "P":
primes.append(i)
# Printing the list of primes
i = 0 # We will count from 0 to 9 for every printed row
print()
for prime in primes:
if i < 10:
print("{0:5d}".format(prime), end="")
i = i + 1
else:
print() # New line after the last (10th) number
i = 0
```

=========== The answer for your EDITED, **totally other** question: ===========

=========== (Please don't do it, create a **new** question instead.) ===========

Replace this part of your code:

```
print ("Product" + " " + "Quantity" +" "+ "Cost")
c = 0
while c < len(product_names):
print (product_names[c] + " " + str(product_costs[c]) + " "+ str(quantity[c]))
c +=1
```

with this:

```
print("{:10s} {:>10s} {:>10s}".format("Product", "Quantity", "Cost"))
for c in range(0, len(product_names)):
print("{:10s} {:10d} {:10d}".format(product_names[c], quantity[c], product_costs[c]))
```

(I changed your order in the second `print`

to *name, quantity, cost* - to correspond with your 1st `print`

.)

Maybe you will want replace `10`

with other numbers, but the triad of numbers in the first `print`

must be the same as in the second `print`

(e. g. `12`

`9`

`6`

).

**The explanation:**

`{: }`

are placeholders for individual strings / integers listed in the `print`

statements in the `.format()`

method.

The *number* in the placeholder express the length reserved for the appropriate value.

The optional `>`

means the output has be *right* aligned in its reserved space, as default alignment for text is *to the left* and for numbers *to the right*. (Yes, `<`

means *left* aligned and `^`

centered.)

The letter in the placeholder means `s`

for a string, `d`

(as "decimal") for an integer - and may be also `f`

(as "float") for numbers with decimal point in them - in this case it would be `{:10.2f}`

for `2`

decimal places (from reserved `10`

) in output.

The conversion from number to string is performed *automatically* for symbols `d`

or `f`

in the placeholder, so `str(some_number)`

is not used here.