【问题标题】:Print word from phrase从短语中打印单词
【发布时间】:2021-02-24 18:00:43
【问题描述】:

目的是首先向用户打印文本“你完美的寒假是包括阳光温暖的沙滩和咸水的海浪,还是鼓舞人心的城市生活?”

在此之后,它要求用户输入一个数字。根据数字,它会从文本中打印该单词。

例如,数字 0 将是 'Does'。

print(‘Does your perfect winter vacation include sun-heated beach sand and salty waves or inspiring city life?’)
phrase = ‘Does your perfect winter vacation include sun-heated beach sand and salty waves or inspiring city life?’
ask = input('Print the word from the phrase, by your given number? ')
x = ask
y = phrase.split()
for i in y()[x]:
     print(i)

【问题讨论】:

  • 1.将x 转换为数字:x = int(ask)。 2. 使用 x 作为索引:print(phrase.split()[x])。不确定循环的用途。
  • 这似乎是一个家庭作业问题,而且措辞很糟糕。此外,没有提出任何问题。你有什么问题?您需要什么帮助?

标签: python for-loop input split


【解决方案1】:

几个建议:

  1. 将短语放入变量中
  2. 索引“x”应转换为整数(因为输入返回一个字符串)
  3. split 函数返回一个列表,所以你不能调用它 (split()())
  4. 使用循环将打印第一个单词的所有字母

一个简单的代码(没有健壮性)如下所示:

phrase = 'Does your perfect winter vacation include sun-heated beach sand and salty waves or inspiring city life?'
print(phrase)
x = int(input('Print the word from the phrase, by your given number? '))
y = phrase.split()
print(y[x])

【讨论】:

  • 嗨,在 StackOverflow 中,通常建议提供解释代码(而不是纯代码)的文本。 ...并且还应避免仅将指向外部网站的链接作为答案(因为链接可能会损坏),但这不适用于此处。
  • 第一次。这就是说,我很感激为未来的职位提供建议。很好的解释,谢谢
猜你喜欢
  • 1970-01-01
  • 2014-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-21
  • 1970-01-01
  • 2021-09-13
  • 2021-05-11
相关资源
最近更新 更多