简体   繁体   English

Python文字游戏,这个None是哪里来的?

[英]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 .我正在尝试制作一个基于文本的游戏,并且为了确保它适合屏幕并且看起来不太糟糕,我使用WrapperFill方法制作了这个小 function 。

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

Now, this works perfectly, but for dialogues I have this other function:现在,这很完美,但对于对话,我有另一个 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.我不确定 None 是从哪里来的。 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.我猜 Fill 在文本末尾传递了一些东西,而 stdout.write 正在写它,但我在一般编程方面是一个菜鸟,特别是在 Python,所以我完全迷路了。

As a side note, I'm passing "."作为旁注,我正在传递“。” and "?"和 ”?” in different elifs instead of writing it like:在不同的 elifs 而不是像这样写:

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

because the sleep seems to take too much time因为睡眠似乎花费了太多时间

Thanks in advance for the help.先谢谢您的帮助。

It comes from print ing the return value of dialogue .它来自print dialogue的返回值。 Since dialogue does not explicitly return anything, the return value is implicitly None .由于dialogue没有显式return任何内容,因此返回值隐式为None

The dialogue function already writes to sys.stdout , so you don't need to print anything; dialogue function 已经写入sys.stdout ,所以你不需要print任何东西; 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. print为你做的一件事就是写一个换行符。 You will need to add that at the end of the dialogue function:您需要在dialogue function 的末尾添加:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM