-6

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.

However, what I can't figure out how to do is , aligning of the printed outputs

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!

Harry Kim
  • 11
  • 3
  • 2
    What have you tried? You're probably not going to find much help in just doing your homework for you. – pvg Nov 18 '16 at 23:36
  • Please review [how to ask homework questions](http://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions). Show us what you've done. – TemporalWolf Nov 18 '16 at 23:41
  • valid = 0 while valid == 0: n = int(input("What is the maximum value that you want to find prime values?")) if n< 10: print ("Your input number should be greater than or equal to 10!") valid = 0 break else: valid = 1 while valid == 1: n_list =[] pn_list = [] for c in range (0,n+1): n_list.append(c) print (n_list) – Harry Kim Nov 19 '16 at 00:02
  • You can [edit] your question to add additional details; don't put code in comments, that doesn't work very well (as you can see). – Martin Tournoij Nov 19 '16 at 00:03
  • https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes This has a lovely little animation. Think of that graph as a list of lists and you are half way there! – lysdexia Nov 19 '16 at 00:22
  • I'm voting to close this question as off-topic because there are already good questions on python implementation of the Sieve of Eratothenes algorithm such as this http://stackoverflow.com/q/3939660/1328439. Here the task is to follow specific instructions and it is unlikely it would be helpful to future readers. – Dima Chubarov Nov 19 '16 at 09:02

1 Answers1

-1

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 (with the original indentation, as it is important in Python):

print("{:15s} {:>15s} {:>15s}".format("Product", "Quantity", "Cost"))

for c in range(0, len(product_names)):
    print("{:15s} {:15d} {:15d}".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.)

Probably you will want change 15's to other numbers (even individually, e. g. to 12 9 6) but the triad of numbers in the first print must be the same as in the second print().

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 {:15.2f} for 2 decimal places (from reserved 15) 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.

Addendum:

If you will have time, please copy / paste your edited version as a new question, then revert this question to its original state, as people commented / answered your original one. I will find your new question and do the same with my answer. Thanks!

MarianD
  • 13,096
  • 12
  • 42
  • 54
  • 1
    I am down voting this answer because Stackoverflow is not a homework plagiarism service. – Christian Dean Nov 19 '16 at 03:36
  • Thanks so much, I will try to review this and re-work on a new version of this ! – Harry Kim Nov 19 '16 at 08:57
  • @MarianD hello , can you try to look at my new edited question and if you can figure out ? :) Thanks so much again ! – Harry Kim Nov 19 '16 at 19:55
  • @HarryKim - I added the answer of your edited question at the end of my edited answer. – MarianD Nov 19 '16 at 21:30
  • Your answer is better, but I'm afraid that the question is still off-topic. I'll remove my down vote this time, but please refrain from answering off-topic home works questions in the future :) – Christian Dean Nov 20 '16 at 04:30
  • @MarianD Hello friend. Thanks for helping me out. However, when I run the codes , it would indicate print("{:15s} {:15d} {:15d}".format(product_names[c], quantity[c], product_costs[c])) ValueError: Unknown format code 'd' for object of type 'float' error . – Harry Kim Nov 20 '16 at 23:27
  • @leaf thank you so much – Harry Kim Nov 20 '16 at 23:27
  • In that `print` use in the 3rd template {:15.2f} (instead of {:15d}) as the numbers in the `product_costs` are not integer (decimal, `d`) but with decimal fraction (float, `f``). See the explanation in my answer. – MarianD Nov 21 '16 at 06:44