`figlet.setFont()` 和 `random.choice()` 相互冲突。

huangapple go评论70阅读模式
英文:

figlet.setFont() and random.choice() conflicting with each other

问题

我在尝试在我的终端中用Python制作figlet文本这是CS50第6周练习问题之一我试图从pyfiglet的字体列表中随机选择一个字体并尝试以以下方式实现

```python
import random
from pyfiglet import Figlet

figlet = Figlet()
figfonts = figlet.getFonts()

# ...

random.seed()
figlet.setFont(random.choice(figfonts)) # 这里出错

然而,当我在我的终端中运行时,我得到以下错误:

TypeError: Figlet.setFont()接受1个位置参数,但给出了2个

我感到困惑。我只给了figlet.setFont()一个参数,为什么它会说有两个?我无法理解错误消息试图告诉我什么是错误的。


<details>
<summary>英文:</summary>

I&#39;m trying to make figlet text in my terminal in python, as per one of the practice problems in week 6 of CS50. I&#39;m trying to have a font picked randomly from pyfiglet&#39;s list of fonts, and I&#39;m trying to implement this as follows:

import random
from pyfiglet import Figlet

figlet = Figlet()
figfonts = figlet.getFonts()

...

random.seed()
figlet.setFont(random.choice(figfonts)) # error here

However, when I run this in my terminal, I get the following error:

`TypeError: Figlet.setFont() takes 1 positional argument but 2 were given`

I&#39;m confused. I&#39;m only providing `figlet.setFont()` 1 argument, why is it saying that there are two? I just can&#39;t piece together what the error message is trying to tell me is wrong.

</details>


# 答案1
**得分**: 2

The method `setFont` [has the signature](https://github.com/pwaller/pyfiglet/blob/d78b6ab6e05bf39370a134f6847b79e28a937b2e/pyfiglet/__init__.py#L847):
```python
def setFont(self, **kwargs)

So passing font as a keyword argument (vs. a positional argument like you tried to) should fix the issue (as suggested by @jasonharper in the comments).

For example:

figlet.setFont(font=random.choice(figfonts))

> I'm confused. I'm only providing figlet.setFont() 1 argument, why is it saying that there are two?

As you can see from the signature above, the first positional argument is self. Python automatically passes the instance (in this case figlet) as the first argument because setFont is "bound" to the instance figlet when called as figlet.setFont(). It's also possible (but unconventional) to call instance methods like so: Figlet.setFont(figlet)

英文:

The method setFont has the signature:

def setFont(self, **kwargs)

So passing font as a keyword argument (vs. a positional argument like you tried to) should fix the issue (as suggested by @jasonharper in the comments).

For example:

figlet.setFont(font=random.choice(figfonts))

> I'm confused. I'm only providing figlet.setFont() 1 argument, why is it saying that there are two?

As you can see from the signature above, the first positional argument is self. Python automatically passes the instance (in this case figlet) as the first argument because setFont is "bound" to the instance figlet when called as figlet.setFont(). It's also possible (but unconventional) to call instance methods like so: Figlet.setFont(figlet)

huangapple
  • 本文由 发表于 2023年4月17日 10:02:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031271.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定