Issue
I am trying to achieve a print where it outputs longest line and the words associated with it.
with open("txt.txt") as file:
for line in file:
words = line.split()
words_count += len(words)
if maxlines == 0 or len(words) > len(maxlines.split()):
maxlines = line
sentences.append(line)
print("Longest line has " + maxlines_len + " words: " + maxlines)
The variable will spit out a typeError if I declare its value it without str(). Is there any workarounds without fstrings or str()?
Thank you!
Solution
Well, you can't sum strings and integers, but print()
happily accepts any number of arguments and will stringify them internally (and separate them with spaces by default; you can control that with the sep=
keyword argument):
print("Longest line has", maxlines_len, "words:", maxlines)
If using f-string formatting was an option (not sure why you wouldn't use them):
print(f"Longest line has {maxlines_len} words: {maxlines}")
Answered By - AKX
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.