How to display certian output while print the result?

Viewed 67

Below is the code I use.

# display a welcome message
print("===============================================================")
print("Shipping Calculator")
print("===============================================================")

while True:

    # get input from the user
    cost_of_items = float(input("Cost of items ordered:  "))

    # make sure input is a positive number
    if cost_of_items < 0:
        print("You must enter a positive number. Please try again.")
        continue
    
    # to do: get shipping cost(one given for example)
    
    if cost_of_items < 30:
        shipping_cost = 5.95
        
    if 30.00 < cost_of_items < 49.99:
        shipping_cost = 7.95
        
    if 50.00 < cost_of_items < 74.99:
        shipping_cost = 9.95
    
    if cost_of_items > 75:
        shipping_cost = 0
        
    # To do: calculate total cost: total_cost
    total_cost = cost_of_items + shipping_cost

    # to do: display in output, print shipping cost, print total cost
    print(shipping_cost,total_cost)
    
    # to do: make your program more interesting, ask user how many item like to ship, 
    num_of_items = float(input("Number of items to ship:  "))
    
    # and caclutate the cost of shipping based on total cost
    total_shipping_cost = shipping_cost*num_of_items
    final_total_cost = cost_of_items + total_shipping_cost
    print(total_shipping_cost, final_total_cost)


    # see if the user wants to continue
    choice = input("Continue? (y/n): ")
    print("===============================================================")
    if choice.lower() != "y":
        break
        
print("Bye!")

Here is the output I get: enter image description here

but I want the output to display like:

enter image description here

How can I do that? to put some description before the output? like Number of items to ship: X, Total Shipping cost: XX

4 Answers

You are attempting to create a multi-line string. That can simply be done with "\n"

Example:

# String containing newline characters
line_str = "I'm learning Python.\nI refer to TechBeamers.com tutorials.\nIt is the most 
popular site for Python programmers."

That should make it appear like:

"I'm learning Python.

I refer to TechBeamers.com tutorials.

It is the most popular site for Python programmers."

need to add the newline character to the end of your prints:

\n

Don't forget to print the name of each variable alongside the value of each variable.

print('Shipping Cost: ' + str(shipping_cost) + '\n', 'Total Cost: ' + str(total_cost))
...
print('Total Shipping Cost: ' + str(total_shipping_cost) + '\n', 'Final Shipping Cost: ' + str(final_total_cost))

You need to write some text manually that you want to print as follows

# display a welcome message
print("===============================================================")
print("Shipping Calculator")
print("===============================================================")

while True:

    # get input from the user
    cost_of_items = float(input("Cost of items ordered:  "))

    # make sure input is a positive number
    if cost_of_items < 0:
        print("You must enter a positive number. Please try again.")
        continue
    
    # to do: get shipping cost(one given for example)
    
    if cost_of_items < 30:
        shipping_cost = 5.95
        
    if 30.00 < cost_of_items < 49.99:
        shipping_cost = 7.95
        
    if 50.00 < cost_of_items < 74.99:
        shipping_cost = 9.95
    
    if cost_of_items > 75:
        shipping_cost = 0
        
    # To do: calculate total cost: total_cost
    total_cost = cost_of_items + shipping_cost

    # to do: display in output, print shipping cost, print total cost
    print("Shipping cost:", shipping_cost)
    print("Total cost:", total_cost)
    
    # to do: make your program more interesting, ask user how many item like to ship, 
    num_of_items = float(input("Number of items to ship:  "))
    
    # and caclutate the cost of shipping based on total cost
    total_shipping_cost = shipping_cost*num_of_items
    final_total_cost = cost_of_items + total_shipping_cost

    print("Total Shipping cost:", total_shipping_cost)
    print("Final total cost:", final_total_cost)


    # see if the user wants to continue
    choice = input("Continue? (y/n): ")
    print("===============================================================")
    if choice.lower() != "n":
        break
        
print("Bye!")
Related