我是不是导入了什么错误的东西
Have I imported something wrong
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)
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)