gpt4 book ai didi

python - 如何在打印结果时显示某些输出?

转载 作者:行者123 更新时间:2023-12-04 03:53:24 26 4
gpt4 key购买 nike

下面是我使用的代码。

# 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!")

这是我得到的输出: enter image description here

但我希望输出显示如下:

enter image description here

我该怎么做?在输出前放一些描述?比如要运送的商品数量:X,总运费:XX

最佳答案

您正在尝试创建多行字符串。这可以简单地用 "\n"

来完成

例子:

# 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."

这应该使它看起来像:

“我正在学习 Python。”

我引用了 TechBeamers.com 教程。

这是最Python 程序员的热门网站。”

关于python - 如何在打印结果时显示某些输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64112618/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com