Yes, concatenate worked for me. I know it’s fairly simple, but I guess I’ve never used concatenate in a print statement, so it threw me off lol. Was drawing a blank.
Also, if I wanted to print these on the same line how would I do that?
print(“Current hand:”), display_hand(hand) ------- These print on separate lines “Current hand” prints before the function.
print(“Current hand:”, display_hand(hand) ) ----- and this prints the return of the function which is None
Where display_hand(hand) function returns None but prints from the function itself?
def display_hand(hand):
for letter in hand.keys():
for j in range(hand[letter]):
print(letter, end=' ') # print all on the same line
print()
When I want any output to have a specific format, I tend to build a ‘output’ string, then print(output) rather than try to do it on-the-fly within the print() function.
What you seem to be looking for is called “string formatting,” a way
of templating a string with placeholders for the things you want to
substitute. There are several kinds of string formatting supported
in modern versions of Python, but the current recommendations in
Python’s official documentation are for {}-style formatting
(additional indentation provided for clarity):
print(
'"{}" Earned {} points, Total Score: {}'.format(
word,
get_word_score(word, n),
total,
)
)
Thank you, I have pinned these docs as well. Simply using concatenate worked for the first issue, although I know going forward, I may need to learn these different string formatting methods mentioned here. I still haven’t found the right syntax for calling this display_hand(hand) function and printing “Current hand:” on the same line. I know I could do this from within the function or I could return something from the function and print it that way but I’d rather not for now if I can find a different way.
The real issue seems to be trying to do this while returning None on my function though.
I guess you can just add the (end = ’ ') argument to a print statement followed by a function call like what was already being used in the function itself. Doesn’t have to be two print statements.
print("Current hand:", end = ' ')
display_hand(hand)
I’m simply exploring the possibilities really. Maybe using a class is not needed as the method is already covered by one (or more) of the five ways your PB list, three of which I’ve come across before, but I’ve only ever used two of them in any of my own code.
I’m trying to get out of the habit of relying on my ‘old school’ ways and learn how to use Python in a way that it’s designed to be used.
Anyway, I’ve cobbled this together as a working example of my thoughts and based on this thread.
class Score:
def __init__(self, word, points):
self.word = word
self.points = points
def output(self):
print(f'"{self.word}" earned {self.points} points')
player1 = Score("blue", 10)
player1.output()