【问题标题】:How can I use two different strings in one placeholder?如何在一个占位符中使用两个不同的字符串?
【发布时间】:2018-08-24 00:05:02
【问题描述】:

我在 Re: 条件语句类中遇到的一个问题遇到了很多麻烦。

我很难理解如何让两个变量("year" 和 "s")能够进入一个 {}。

我必须显示一个人的年龄(以他们的出生年份为准):

计算出用户的出生年份并显示适当的 下方留言

<FirstName>, it is now <CurrentYear>, you are <Age> year old and so you were born in <YearOfBirth>.

<FirstName>, it is now <CurrentYear>, you are <Age> years old and so you were born in <YearOfBirth>.

上面使用的信息在语法上必须符合年龄。

  • &lt;FirstName&gt; 是用户的名字。
  • &lt;CurrentYear&gt; 是当前年份
  • &lt;Age&gt; 是用户的年龄。
  • &lt;YearOfBirth&gt; 是用户的出生年份。

以及他们给出的一些例子:

Please enter your first name: Jane
Hi Jane, please enter your age in years: 1
Jane, it is now 2018, you are 1 year old and so you were born in 2017.

和

Please enter your first name: Elaine
Hi Elaine, please enter your age in years: 22
Elaine, it is now 2018, you are 22 years old and so you were born in 1996.

我必须使用以下字符串:

 "{}, it is now {}, you are {} {} old and so you were born in {}."
 "year"
 "s"

【问题讨论】:

  • 我投票决定将此问题作为离题结束,因为 SO 不是代码编写或教程服务。请展示你的努力
  • @EdChum 引用的{} 指的是字符串格式变量,因此第一次谷歌点击是无用的。我想说的是保持问题开放,毕竟他们并没有要求我们编写代码。
  • 话虽如此,@sacker,这个问题包含很多说明。尽量在每个帖子中保留一个可回答的问题,并且只提供与此完全相关的信息。
  • @Arne 很公平,但这个问题仍然过于宽泛,因为它需要帮助来理解他们的任务,所以它甚至还没有达到编码阶段,编程是 SO 的主要推动力
  • 我已经大大减少了你的问题文本。请注意这是如何编辑的;我把你的问题放在首位,所以阅读这篇文章的人知道要寻找什么。我从作业文本中删除了与您的实际问题无关的所有内容。尝试在以后的问题中做同样的事情。

标签: python boolean conditional


【解决方案1】:

你的帖子包含很多信息和隐含的问题,所以我将把我的答案限制在明确的问题上:

我很难理解如何让两个变量(“year”和“s”)进入一个 {}。

将包含1 year 或[more than one] years 的字符串是:"{}, it is now {}, you are {} {} old and so you were born in {}.",它方便地包含数字和“年”或“年”的两个格式占位符。举一个最小的例子,这可能会让你知道如何解决你的问题:

>>> s = "you are {} {} old"
... options = [0, 1, 2, 3]
... for op in options:
...     year_label = "year"
...     if op != 1:
...         year_label += "s"
...     print(s.format(op, year_label))

打印:

you are 0 years old
you are 1 year old
you are 2 years old
you are 3 years old

可能有更多描述性的方式来写这个,我不知道它是否适合所有可能的输入。但重要的一点是,并非输出中的每个占位符都映射到一个输入或输出变量。在这种情况下,您需要根据输入推导出一个值。

【讨论】:

  • 他们被指示使用字符串"year"和"s",你使用"year"和"years"。
  • 我很确定这些说明暗示他们应该使用if 语句,而不是条件表达式。 year_label = "year",然后是if age &gt; 1: year_label += "s"。
  • 我认为你是对的。现在我想一想,向刚开始学习 python 的人建议三元运算符可能一开始就不是一个好的选择。感谢您发现它。
【解决方案2】:

根据您需要的占位符,这里有一些代码:

year = 2018



name = input("Please enter your first name: ")

age = input("Hi " + name.title() + " , please enter your age in years: ")

age = int(age)

age_minus_year = (year - age)

if age != 1:

    print("{}, it is now {}, you are {}{} old and so you were born in {}.".format(name.title(), year, age, ' years', str(age_minus_year)))

else:

    print("{}, it is now {}, you are {}{} old and so you were born in {}.".format(name.title(), year, age, ' year', str(age_minus_year)))

这是你的输出:

Please enter your first name: jane
Hi jane, please enter your age in years: 1
Jane, it is now 2018, you are 1 years old and so you were born in 2017.

根据您提到的代码所需的内容,此代码应为您提供所需的格式。您只需在此处使用 .format 方法。 if 语句将根据用户输入的内容简单地确定打印语句是否应该声明年份或年份。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    • 2016-03-13
    • 2016-10-24
    • 2014-05-02
    • 1970-01-01
    相关资源
    最近更新 更多