如何为句子分配数字?
How do I assign numbers to sentences?
我正在尝试这样做
proverbs = "a bad excuse is better than none","A bad workman blames his tools," etc.
number_of_proverb = random.randint (1,38)
proverb = sentence number number_of_proverb
但是我不知道如何给句子分配数字。预先感谢您的所有帮助。
PS38因为有38个句子。也是这个
proverbs = "something something","something something","something, something"
正确吗?很抱歉这些基本问题,但我才刚刚开始。
我明白你想做什么了。你正在制作一个谚语列表,你想从列表中随机 select 一个。因此,最好将您的谚语存储在一个列表中,并使用 random.choice
到 select 列表中的它们。
proverbs = ["this proverb","that proverb","something, something", "another"]
random.choice(proverbs)
- How to randomly select an item from a list?
如果你想有一个数字与谚语相关联,你可以只使用index
函数来获取谚语在列表中的位置。像这样:
proverbs.index("another")
输出:
3
- Finding the index of an item in a list
谢谢罗伯特和@andrbrue。我做到了。
proverbs = ["A bad excuse is better than none.","A bad workman blames his tools."
(有38条谚语)
rp = (proverbs[random.randint(1,38)])
我还有一些问题,但我会解决的。非常感谢
我正在尝试这样做
proverbs = "a bad excuse is better than none","A bad workman blames his tools," etc.
number_of_proverb = random.randint (1,38)
proverb = sentence number number_of_proverb
但是我不知道如何给句子分配数字。预先感谢您的所有帮助。
PS38因为有38个句子。也是这个
proverbs = "something something","something something","something, something"
正确吗?很抱歉这些基本问题,但我才刚刚开始。
我明白你想做什么了。你正在制作一个谚语列表,你想从列表中随机 select 一个。因此,最好将您的谚语存储在一个列表中,并使用 random.choice
到 select 列表中的它们。
proverbs = ["this proverb","that proverb","something, something", "another"]
random.choice(proverbs)
- How to randomly select an item from a list?
如果你想有一个数字与谚语相关联,你可以只使用index
函数来获取谚语在列表中的位置。像这样:
proverbs.index("another")
输出:
3
- Finding the index of an item in a list
谢谢罗伯特和@andrbrue。我做到了。
proverbs = ["A bad excuse is better than none.","A bad workman blames his tools."
(有38条谚语)
rp = (proverbs[random.randint(1,38)])
我还有一些问题,但我会解决的。非常感谢