I have attached a trimmed down version of the program. The issue I'm having is when I print N there is an extra * at the end that I haven't managed to remove from the code. I've been trying for hours now but I just can't seem to crack it.
我附上了一个精简版的程序。我遇到的问题是,当我打印N时,在末尾有一个额外的 *,我没有设法从代码中删除。我已经试了好几个小时了,但似乎就是破解不了。
I've changed +'s to -'s and vice versa. I've tried increasing and decreasing values. All I've managed to do is warp my output, but the extra * remains.
我已经将+‘S改为-’S,反之亦然。我尝试了增减值。我所做的只是扭曲了我的输出,但多余的*仍然存在。
Program:-
计划:-
def is_valid_word(word):
valid_letters = set("ABNOTY")
return all(letter in valid_letters for letter in word)
size=7
def get_pattern(letter, size):
# Define patterns for each letter based on the specified size
patterns = {
'N': [" " + "*" + " " * (size - 2) + "*"] + [" " + "*" + " " * (i - 1) + "*" + " " * (size - i - 2) + "*" + " " for i in range(1, size)],
}
return patterns.get(letter, ["Invalid"])
def main():
while True:
word = input("Enter a valid word (or 'END' to stop): ")
if word.upper() == "END":
break
if not is_valid_word(word):
print("Error: Invalid input. Please enter a valid word.")
continue
letter_patterns = [get_pattern(letter, size) for letter in word]
for i in range(size):
for pattern in letter_patterns:
print(pattern[i], end=" ")
print()
if __name__ == "__main__":
main()
What I am currently printing is
Output with extra * on bottom right
我当前打印的是在右下角带有额外*的输出
更多回答
If I run your code it fails with IndexError: list index out of range
at print(pattern[i], end=" ")
.
如果我运行您的代码,它将失败,并显示IndexError:list index out of range at print(Patter[i],end=“”)。
@Paul Did you try to enter more than one N
? There is another one issue with indentation in the output/rendering (for following letters).
@Paul您是否尝试输入多个N?输出/呈现中的缩进还有一个问题(对于后面的字母)。
@RomanPerekhrest, Thanks for the heads up. I will amend my code to avoid skewing further outputs!
@RomanPerekhrest,谢谢你的提醒。我将修改我的代码,以避免进一步歪曲输出!
优秀答案推荐
to print N you currently have 1 "pattern" for 1st line, then another for the rest of them. And this fails for the last line, which obviously has same pattern as the first one.
要打印N,当前第一行有1个“模式”,其余行有另一个“模式”。最后一行显然与第一行具有相同的模式,这一点失败了。
Updating your get_pattern
to this code will do the trick:
将GET_Pattera更新为下面的代码将会起到作用:
def get_pattern(letter, size):
# Define patterns for each letter based on the specified size
patterns = {
'N': [" " + "*" + " " * (size - 2) + "*"] + [" " + "*" + " " * (i - 1) + "*" + " " * (size - i - 2) + "*" + " " for i in range(1, size - 1)] + [" " + "*" + " " * (size - 2) + "*"],
}
return patterns.get(letter, ["Invalid"])
output:
输出:
Enter a valid word (or 'END' to stop): N
* *
** *
* * *
* * *
* * *
* **
* *
Enter a valid word (or 'END' to stop):
更多回答
Try to enter NNN
on your version and see the output
尝试在您的版本中输入nnn并查看输出
@ilias-sp, Thank you so much! Something so simple.
@Ilias-SP,非常感谢!这么简单的事情。
我是一名优秀的程序员,十分优秀!