Python在循环中使用random -由用户操作分隔



我是编程新手,现在开始学习python课程。我正试着做一个文字宾果游戏,但似乎不能使它工作。

import random
from random import randint
print "Let's play Bingo!"
print
# prompt for input
bingo = input("First enter your bingo words: ") 
# split up the sentence into a list of words
list = bingo.split()
print 
print "Okay, let's go! "
random.shuffle(list)
for choice in random.shuffle(list):
user = raw_input()
if user == "":
print(choice)
raw_input("")

else:
print "That's the end of the game ^.^"
break
#for words in range(len(list)):
#user = raw_input()
#if user == "":
#print(random.sample(list, 1))
#raw_input("")
#else:
#print "That's the end of the game ^.^"
#break

如果我在random.shuffle(list)中使用choice,我会得到NonType错误

之前,我使用了一个for循环。示例(见最后的##部分)这个方法有效,只是在每次迭代中,单词仍然重复。

我试着搜索类似的问题,但它们要么有数字,要么有更多的自动循环。

我想让用户输入单词,然后每次他们按回车键,一个新的单词从列表中出现,而不重复。我似乎不知道如何让它进入一个循环-有什么帮助吗?

我尝试使用random。选择和随机。样本,但单词仍然在for循环中不断重复。尝试洗牌,出现nonType错误

注释:

  • 不要使用list作为变量名,它是Python中用于类型列表的关键字
  • random.shuffle(l)就地操作(即调用后,l将被洗牌)。所以,你只是提供l到循环中。
import random
from random import randint
print "Let's play Bingo!"
print
# prompt for input
bingo = input("First enter your bingo words: ") 
# split up the sentence into a list of words
l = bingo.split()
print 
print "Okay, let's go! "
random.shuffle(l)
for choice in l:
user = raw_input()
if user == "":
print(choice)
raw_input("")
else:
print "That's the end of the game ^.^"
break

公立小学你为什么决定使用Python 2?如果您是Python新手,那么使用Python 3可能会更好。这是你的决定。我们通知你,https://www.python.org/doc/sunset-python-2/: ~:文本= % 20 % 20决定% % 20 1月20,% 20很快% 20 % 20你% 20。

最新更新