'Python text game, where does this None come from?

I'm trying to make a text based game, and, to make sure it fits in the screen and doesn't look too bad, I made this little function using the Fill method from Wrapper.

def reescalate_text(string):
     str = fill(string, width=110)
     return str

Now, this works perfectly, but for dialogues I have this other function:

def dialogue(text):
    text = fill(text, width=80)
    for i in text:
         sys.stdout.write(i)
         sys.stdout.flush()
         time.sleep(0.03)
         if i == ',':
             time.sleep(0.07)
         elif i == '.':
             time.sleep(0.42)
         elif i == '?':
             time.sleep(0.42)
         elif i == '!':
             time.sleep(0.62)

Just passing descriptions and stuff works great, but when I try to send a dialogue, it prints something like:

Hey, you! You're finally awake. You were trying to cross the border, right?None

I send the text to both functions like:

print(reescalate_text('You wake up in a cart, hands tied'))
print()
print(dialogue('Hey, you! You're finally awake. You were trying to cross the border, right?'))

I'm not sure where the None is coming from. I'm guessing that Fill is passing something at the end of text and stdout.write is writing it, but I'm a noob at programming in general and at Python in particular, so I'm completely lost.

As a side note, I'm passing "." and "?" in different elifs instead of writing it like:

if i == '.' or '?':

because the sleep seems to take too much time

Thanks in advance for the help.



Solution 1:[1]

It comes from printing the return value of dialogue. Since dialogue does not explicitly return anything, the return value is implicitly None.

The dialogue function already writes to sys.stdout, so you don't need to print anything; just call it like this:

dialogue('Hey, you! You're finally awake. You were trying to cross the border, right?')

The one thing print was doing for you is to write a newline. You will need to add that at the end of the dialogue function:

    sys.stdout.write('\n')
    sys.stdout.flush()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thomas