输入Python后没有出现额外的换行符

问题描述 投票:0回答:3

这是所需的输出。

Number of Econ motorcycle: 2
Number of Standard motorcycle: 1
Number of Premium motorcycle: 1
Rental duration (number of days): 4
# I want a newline here

Summary of your car rental for 4 day(s)
Economy      2  $320.00
Standard     1  $280.00
Premium      1  $400.00
SUV          0  $0.00
Subtotal     4  $1000.00
Total (18% tax) $1180.00

我尝试过: 打印() 打印(字符(10) """ """ - 多行字符串

这一切似乎都不起作用。

def get_car_rental_booking():
    # Prompt user number of cars for each type and rental duration
    noOfeconomy = int(input("Number of Economy motorcycle: "))
    noOfstandard = int(input("Number of Standards motorcycle: "))
    noOfpremium = int(input("Number of Premium motorcycle: "))
    print("Rental duration (number of days): ", end="")
    rentalDuration = int(input("Rental duration (number of days): "))

 
    print(chr(10))
    # Compute the cost for each motorcycle type
    costEconomy = noOfeconomy * 40 * rentalDuration
    costStandard = noOfstandard * 70 * rentalDuration
    costPremium = noOfpremium * 100 * rentalDuration

    # Compute the total cost including tax 
    subTotal = costEconomy + costStandard + costPremium + costSuv
    total = subTotal * 1.18

    # There is an empty line after getting the inputs.
    dollarSign = "$"
    newLine = "\n"
    print(f'\n\n{newLine}Summary of your motorcycle rental for {rentalDuration} day(s)')
    costEconomy_string = dollarSign + format(costEconomy, '.2f')
    print(f"{'Economy':<13} {noOfeconomy:^3} {costEconomy_string:>10}")
    costStandard_string = dollarSign + format(costStandard, '.2f')
    print(f"{'Standard':<13} {noOfstandard:^3} {costStandard_string:>10}")
    costPremium_string = dollarSign + format(costPremium, '.2f')
    print(f"{'Premium':<13} {noOfpremium:^3} {costPremium_string:>10}")
    costSubtotal_string = dollarSign + format(subTotal, '.2f')
    print(f"{'Subtotal':<13} {rentalDuration:^3} {costSubtotal_string:>10}")
    costTotal_string = dollarSign + format(total, '.2f')
    print(f"{'Total (18% tax)':16}  {costTotal_string:>10}")

我尝试过: 打印() 打印(字符(10) """ """ - 多行字符串

我期望在输入之后和打印语句之前有一个间隙。

python input newline
3个回答
1
投票

我知道您有一些间距和轮廓格式,例如:

f"{'Premium':<13} {noOfpremium:^3} {costPremium_string:>10}"

但也许您可以清理并调整

string.Template
来为您完成大部分打印工作。至少,它有助于从打印输出/摘要中抽象出您的计算逻辑。

尝试根据您的需求扩展这个简化的示例:

from string import Template

tmp = Template('''
Number of Economy cars: $economy_cars_count
Number of Standard cars: $standard_cars_count
Number of Premium cars: $premium_cars_count
Number of SUVs: $suv_cars_count
Rental duration (number of days): $rental_duration_days

Summary of your car rental for 4 day(s)
Economy      $economy_cars_count  $$$economy_cars_rental_cost
Standard     $standard_cars_count  $$$standard_cars_rental_cost
Premium      $premium_cars_count $$$premium_cars_rental_cost
SUV          $suv_cars_count  $$$suv_cars_rental_cost
Subtotal     4  $$$subtotal
Total ($tax_percentage% tax) $$$total
''')

s = tmp.substitute(dict(
    economy_cars_count=1,
    standard_cars_count=2,
    premium_cars_count=3,
    suv_cars_count=4,
    rental_duration_days=4,
    economy_cars_rental_cost=10,
    standard_cars_rental_cost=20,
    premium_cars_rental_cost=30,
    suv_cars_rental_cost=40,
    subtotal=500,
    tax_percentage=18,
    total=590
))
print(s)
Number of Economy cars: 1
Number of Standard cars: 2
Number of Premium cars: 3
Number of SUVs: 4
Rental duration (number of days): 4

Summary of your car rental for 4 day(s)
Economy      2  $10
Standard     1  $20
Premium      1  $30
SUV          0  $40
Subtotal     4  $500
Total (18% tax) $590

查看https://docs.python.org/3/library/string.html#template-strings


0
投票

您可以在用户完成输入后打印换行符,方法是添加:

print("\n")

用户提交输入后。

在字符串中使用

\n
创建一个新行。


0
投票

谢谢大家回答这个问题。我可能已经弄清楚问题出在哪里了。

我使用在线Python解释器来解释我的代码,但它没有给出我想要的输出。不过,它在 VScode 上运行良好。

所以你运行代码的平台很重要。谢谢大家!我真是个新手。

© www.soinside.com 2019 - 2024. All rights reserved.