python - 我怎样才能让程序继续进行?

标签 python

我对编程和Python非常陌生。该程序的目标是在输出时将用户句子显示为三角形。到目前为止我所拥有的是这个

def main():
  enteredSentence = input('Enter a short sentence: ')
  count = 0
  return enteredSentence, count


#prints the sentence in a triangle from first letter to full sentence
def UP(enteredSentence,count):
  for count in (enteredSentence):
    count = count + 1
  print(enteredSentence[0:count])
  print(enteredSentence)

# prints the sentence in a triangle from full sentence to first letter
def Down(enteredSentence, count):
  for count in enteredSentence:
    count = count - 1
  print(enteredSentence [0:count])


# prints out a message with the total count of letters printed.
#def Count():
main()

程序运行,但只接受输入,然后停止

最佳答案

def main():
    input_sentence = input('Enter a short sentence: ')
    count = 0
    up(input_sentence, count)
    down(input_sentence, count)


# prints the sentence in a triangle from first letter to full sentence
def up(input_sentence, count):
    for letter in (input_sentence):
        count += 1
        print(input_sentence[0:count])
    print(input_sentence)

# prints the sentence in a triangle from full sentence to first letter
def down(input_sentence, count):
    for letter in input_sentence:
        count -= 1
        print(input_sentence[0:count])


# prints out a message with the total count of letters printed.
if __name__ == '__main__':
    main()

产量:

$ Enter a short sentence: hello


h
he
hel
hell
hello
hello
hell
hel
he
h

我让代码更加“Pythonic”。看看here

关于python - 我怎样才能让程序继续进行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58780511/

相关文章:

python - 亚马逊搜索 API

python - 将 header 值传递给 django 中的 get 请求

python - 用于 postgresql 的驱动程序 python

python - 如何 pickle Keras 模型?

python - matplotlib 的平滑、插值的三级或四级色标(例如 r、g、b 三角形)?

python - 比较两个列表之间相同索引的项目并将最小值附加到第三个列表?

python - 类型错误 : Neo4j does not support PackStream parameters of type int64

python - numpy.loadtxt - 否定usecols?

python - dict.__setitem__(key, x) 是否比 dict[key] = x 慢(或快),为什么?

python - 画面 : How to automate publishing dashboard to Tableau server