繁体   English   中英

从短语中打印单词

[英]Print word from phrase

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

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

例如,数字 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. 将短语放在变量中
  2. 索引“x”应转换为 integer(因为输入返回一个字符串)
  3. split function 返回一个列表,因此您不能调用它( 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])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM