【问题标题】:Python: Trying to get user input from moving outside border around on business cardPython:试图通过在名片上移动外部边界来获取用户输入
【发布时间】:2015-04-02 06:58:02
【问题描述】:
first_name = input("What is your first name?")

last_name = input("What is your last name?")

phone_number = input("What is your phone #?")

one = ('|')
two = ('|')
print('Here is your business card...')

business_card = """
+------------------------------------------------+
|    |                                           |
|   -|          %s, %s                           %s
|  --|          Tribute Liabilities Associate    |
| ---|          Parasail Capital                 |
| ---------                                      |
|  -------      4 Hunger Plaza                   |
|               STE 1400                         |
|               District 12, Panem 00012         | 
|                                                |
| Work: %s  @: champ@parasail.com                %s
+------------------------------------------------+""" % (first_name, last_name, one , phone_number, two)



print (business_card)

print('Here is your business card...')

尝试在不移动边框的情况下在名片上输入名字/姓氏和电话号码。由于名称和/或电话号码产生的空白区域,它会自动移动边框(向左或向右)......有什么想法吗?

【问题讨论】:

    标签: python border whitespace indentation quotes


    【解决方案1】:

    如果您使用 format() 字符串方法,您可以告诉它在您输入的字符串的任一侧放置多少空白。

    这是 format() 中涉及的迷你语言的文档: https://docs.python.org/2/library/string.html#formatspec

    因此,当您输入名称时,您会执行"{:15}".format(name) 之类的操作,其中15 是您允许它填充的空格数(输入默认为左对齐)。与"%s" % (name) 相比,这为您提供了更多控制权,并允许更轻松地进行格式化。

    一般来说,format() 文档将包含大量有关如何动态格式化字符串的信息。

    【讨论】:

      猜你喜欢
      • 2018-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-04
      • 2017-05-01
      • 2015-01-23
      相关资源
      最近更新 更多