Python 3-如何从命令行应用程序中删除空白行

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

我正在为Beginning Python类进行作业。分配非常简单,可以接收名字,姓氏,地址行1,地址行2,城市,州,邮编并将其打印出来,但仅在包含信息的情况下才打印地址行2,否则请跳过。我制定了一些代码来使其工作,但是问题是,如果地址行2中没有数据,则在打印信息时控制台将显示一个空行。然后在下一行上打印城市,州和邮政编码。这是学校的在线课程,所以我基本上是在自学,并且随我所愿地进行练习。我将不胜感激你们能给我的任何帮助。

title = "Address Application"
print(title)

def main():
    global fname
    global lname
    global address1
    global address2
    global city
    global state
    global zip
    fname = input("Enter your first name: ")
    lname = input("Enter your last name: ")
    address1 = input("Enter your street address: ")
    address2 = input("If you had an additional address line, enter: ")
    city = input("Enter your city: ")
    state = input("Enter your 2 letter state abbreviation: ")
    while len(state) != 2:
        state = input("Please enter the 2 letter state abbreviation: ")
        if len(state) ==2:
            break

    zip = input("Enter your zip code: ")
    beautify()

# This function cleans up the user's input and outputs with proper capitalization
def beautify():
    fname_cap = fname.capitalize()
    lname_cap = lname.capitalize()
    address1_cap = address1.title()
    address2_cap = address2.title()
    city_cap = city.capitalize()
    state_cap = state.upper()
    print("=" * 80)
    print(fname_cap, lname_cap)
    print(address1_cap)
    if address2_cap != None:
        print(address2_cap)
    print(city_cap,",", state_cap, zip)
    print("=" * 80)
    end()

def end():
    end_result = input("Would you like to enter another address? Y or N ")
    if end_result.lower() == "n":
        exit()
    else:
        print()
        print("*" * 80)
        main()

main()

enter image description here

python
3个回答
0
投票

当用户未输入address2时,address2的值是空字符串而不是。这样,您会看到空白。

因此,请尝试

if address2_cap != '':
        print(address2_cap)

代替

if address2_cap != None:
        print(address2_cap)

0
投票
title = "Address Application"
print(title)

def main():
    global fname
    global lname
    gLobal address1
    global address2
    global city
    global state
    global zip
    fname = input("Enter your first name: ")
    lname = input("Enter your last name: ")
    address1 = input("Enter your street address: ")
    address2 = input("If you had an additional address line, enter: ")
    city = input("Enter your city: ")
    state = input("Enter your 2 letter state abbreviation: ")
    while len(state) != 2:
        state = input("Please enter the 2 letter state abbreviation: ")
        if len(state) ==2:
            break

    zip = input("Enter your zip code: ")
    beautify()

此功能以适当的大写字母清除用户的输入和输出

def beautify():
    fname_cap = fname.capitalize()
    lname_cap = lname.capitalize()
    address1_cap = address1.title()
    address2_cap = address2.title()
    city_cap = city.capitalize()
    state_cap = state.upper()
    print("=" * 80)
    print(fname_cap, lname_cap)
    print(address1_cap)
    if (len(address2_cap) != 0):
        print(address2_cap)
    print(city_cap,",", state_cap, zip)
    print("=" * 80)
    end()

def end():
    end_result = input("Would you like to enter another address? Y or N ")
    if end_result.lower() == "n":
        exit()
    else:
        print()
        print("*" * 80)
        main()

main()


0
投票

[对address2使用真实的检查,例如if address2而非if address2 != None

我也建议避免使用global,并且通常减少变量的使用。

def main():
    print("Address Application")
    fname = input("Enter your first name: ")
    lname = input("Enter your last name: ")
    address1 = input("Enter your street address: ")
    address2 = input("If you had an additional address line, enter: ")
    city = input("Enter your city: ")
    state = ""
    while len(state) != 2:
        state = input("Please enter the 2 letter state abbreviation: ")
    zip_code = input("Enter your zip code: ")
    beautify(
        fname,
        lname,
        address1,
        address2,
        city,
        state,
        zip_code
    )
    if should_repeat():
        main()


def beautify(
    fname,
    lname,
    address1,
    address2,
    city,
    state,
    zip_code
):
    """
    This function cleans up the user's input and pretty-prints it
    """
    print("=" * 80)
    print(fname.capitalize(), lname.capitalize())
    print(address1.title())
    if address2:
        print(address2.title())
    print(f"{city.capitalize()}, {state.upper()} {zip_code}")
    print("=" * 80)


def should_repeat():
    if input(
        "Would you like to enter another address? Y or N "
    ).lower() == "n":
        return False
    print()
    print("*" * 80)
    return True


if __name__ == '__main__':
    main()
© www.soinside.com 2019 - 2024. All rights reserved.