简体   繁体   English

如何从 python 中的输入随机 select

[英]How to randomly select from an input in python

N=input()       #Ex DOG,CAT,MIX,SUN,ELF
print(random.choice(N))

What came out was only a letter, how can i get a whole word?出来的只是一个字母,我怎么能得到一个完整的词呢?

random.choice treats the input as a list of characters. random.choice将输入视为字符列表。 If you want a random word use this:如果你想要一个随机词使用这个:

import random
N=input().split(",")
print(random.choice(N))

Is this what you're after?这就是你想要的吗?

import random

N=input('write some words: ')       #Ex DOG,CAT,MIX,SUN,ELF
N = N.split(' ')    # split the input words into a list

print(random.choice(N))

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

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