【发布时间】:2020-06-11 15:11:51
【问题描述】:
我正在开发一个程序,可以打印单词的所有可能组合。一切正常,但我想更进一步,因此它不仅会打印单词的所有组合。它应该删除一个单词的最后一个字符,直到只剩下一个字母。
这是我写的:
# Enter Word
print("Enter your word:")
print()
s = input(colorGreen)
# If s is a string, print all combinations
if all(s.isalpha() or s.isspace() for s in s):
t=list(itertools.permutations(s,len(s)))
for i in range(0,len(t)):
print(colorGreen + "".join(t[i]))
while len(s) != 1:
t=list(itertools.permutations(s,len(s)))
for i in range(0,len(t)):
print(colorGreen + "".join(t[i]))
print()
print("Finished!")
print()
input("Press anything to quit...")
# If s is not a string, print error
if not all(s.isalpha() or s.isspace() for s in s):
print(colorRed + "You did not enter a correct word")
print("Only use a word for Word combinations")
print("Please restart the program.")
print()
input("Press anything to quit...")
提前致谢:D
【问题讨论】:
-
如果你想
remove the last character of a word until there is only one letter left,你应该做一个最小的例子。这段代码的其余部分似乎与您的问题无关,而且很难看出您在哪里尝试这样做。 -
代码有什么问题?
-
您可以使用切片符号
word[:-1]来获取除最后一个以外的所有字符。word = word[:-1]
标签: python python-3.x string variables