我对编程和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()
程序运行,但只接受输入然后停止
答案 0 :(得分:0)
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