我导入了错误的东西吗?


from random import shuffle
mylist = [' ','O',' ']
my_new_list = shuffle_list(mylist)
guess = player_guess
check_guess(my_new_list,guess)
def shuffle_list(list):
shuffle(list)
return list
def player_guess():

guess = ''
while guess not in ['0','1','2']:
guess = input("pick a number: 0 , 1 , 2")
return int(guess)
def check_guess(mylist,guess):
if mylist[guess] == 'O':
print("Correct!")
else:
print("Wrong guess!")
print(mylist)

我收到这个错误

NameError                                 Traceback (most recent call last)
Input In [2], in <cell line: 5>()
1 from random import shuffle
3 mylist = [' ','O',' ']
----> 5 my_new_list = shuffle_list(mylist)
7 guess = player_guess
9 check_guess(my_new_list,guess)
NameError: name 'shuffle_list' is not defined

Python从上到下读取脚本,所以当它到达调用shuffle_list的行时,Python还不知道shuffle_list是什么,因此它抛出异常。你所需要做的就是把函数调用的行移到底部。

from random import shuffle
mylist = [' ','O',' ']
def shuffle_list(list):
shuffle(list)
return list
def player_guess():

guess = ''
while guess not in ['0','1','2']:
guess = input("pick a number: 0 , 1 , 2")
return int(guess)
def check_guess(mylist,guess):
if mylist[guess] == 'O':
print("Correct!")
else:
print("Wrong guess!")
print(mylist)
# All of the code below can't run until the functions have been defined 
my_new_list = shuffle_list(mylist)
guess = player_guess   # You probably want to call this e.g. player_guess()
check_guess(my_new_list,guess)

相关内容

  • 没有找到相关文章

最新更新