- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我对编码很陌生,我一直在尝试制作一个带有菜单的基于文本的游戏。游戏本身工作正常,但一旦我尝试合并菜单,我收到错误“NameError:在封闭范围内分配之前引用了自由变量'player_one_rps'”。我已经疯狂地用谷歌搜索它有一段时间了,但我找到的几个答案使用了太高级的代码,我还无法理解它。
(我尝试更改范围和缩进,我尝试在不同的缩进处调用不同的函数,我尝试为函数分配一个参数,还尝试将主菜单作为代码中的最后一个函数 - 列表还在继续。 .)
这是菜单和游戏选项 1 的代码:
定义主函数(): print("\t\t*** 欢迎来到这个完全足够的游戏!***")
def game_menu():
"""Displays game menu and prompts user for input"""
menu_choice = input("""What do you want to do?
1 - One player: rock, paper, scissor, lizard, spock
2 - Two player: rock, paper, scissor, lizard, spock
3 - Surprise! Bonus feature
4 - User guide
5 - Quit
Enter the menu number to access: """)
while True:
if menu_choice == "1":
print("One player: rock, paper, scissor, lizard, spock")
player_one_rps()
break
elif menu_choice == "2":
print("Two player: rock, paper, scissor, lizard, spock")
player_two_rps()
break
elif menu_choice == "3":
print("Surprise! Bonus feature")
dad_jokes()
break
elif menu_choice == "4":
print("User guide")
user_info()
elif menu_choice == "5":
print("Quit game")
exit()
elif menu_choice != 1 - 5:
print("Error, choose a valid number")
# print(menu_choice)
game_menu()
main()
# First game
def player_one_rps():
"""One player rock, paper, scissor, lizard, spock - game"""
import random
def instructions():
"""Displays menu and simple instructions on how to play"""
print("Welcome to rock, paper, scissor, lizard, spock!")
play = input("\nNavigate by \"yes\", \"no\", and numbers.\nNew game?:").lower()
if play == "yes":
print("1. Rock")
print("2. Paper")
print("3. Scissors")
print("4. Lizard")
print("5. Spock")
elif play != "no":
print("an error has occured. Please type \"yes\" or \"no\":")
instructions()
def get_user_choice():
"""Prompts the player to pick a 'weapon'"""
choice = int(input("What do you choose?: "))
if choice > 5:
print("Invalid number, please try again....")
get_user_choice()
elif choice < 1:
print("Invalid number, please try again....")
get_user_choice()
elif choice == 1:
print("You chose rock")
elif choice == 2:
print("You chose paper")
elif choice == 3:
print("You chose scissor")
elif choice == 4:
print("You chose lizard")
elif choice == 5:
print("You chose spock")
return choice
def get_pc_choice():
"""The computer chooses a random weapon"""
choice = random.randint(1, 5)
if choice == 1:
print("PC chose rock")
elif choice == 2:
print("PC chose paper")
elif choice == 3:
print("PC chose scissor")
elif choice == 4:
print("PC chose lizard")
elif choice == 5:
print("PC chose spock")
return choice
def winner(user_choice, pc_choice, user_wins, pc_wins, ties):
"""Calculates if the player or computer won the match"""
if user_choice == 1 and pc_choice == 3 or pc_choice == 4:
print("\nYou win.")
user_wins = user_wins.append(1)
elif user_choice == 2 and pc_choice == 1 or pc_choice == 5:
print("\nYou win.")
user_wins = user_wins.append(1)
elif user_choice == 3 and pc_choice == 2 or pc_choice == 4:
print("\nYou win.")
user_wins = user_wins.append(1)
elif user_choice == 4 and pc_choice == 2 or pc_choice == 5:
print("\nYou win.")
user_wins = user_wins.append(1)
elif user_choice == 5 and pc_choice == 1 or pc_choice == 3:
print("\nYou win.")
user_wins = user_wins.append(1)
elif user_choice == pc_choice:
print("\nTie")
ties = ties.append(1)
else:
print("\nPC won")
pc_wins = pc_wins.append(1)
return
def game_total(user_wins, pc_wins, ties):
"""Displays the total score"""
user_wins = sum(user_wins)
pc_wins = sum(pc_wins)
ties = sum(ties)
print("Your final score: ", user_wins)
print("PC\'s final Score: ", pc_wins)
print("Total ties: ", ties)
def main_one_p():
"""Main instructions for how the game runs"""
user_choice = 0
user_wins = []
pc_choice = 0
pc_wins = []
ties = []
final_user_wins = 0
final_pc_wins = 0
final_ties = 0
Continue = "yes"
instructions()
while Continue == "yes":
user_choice = get_user_choice()
pc_choice = get_pc_choice()
winner(user_choice, pc_choice, user_wins, pc_wins, ties)
Continue = input("Would you like to play again: ").lower()
if Continue == "no":
print("This is the final scores.")
break
game_total(user_wins, pc_wins, ties)
main_one_p()
player_one_rps()
game_menu() # Returns player to the main menu
(抱歉,如果它很长)
有人可以帮我指出我的错误方向吗?有关如何修复它的解释和提示也将不胜感激:)
总的来说,我很感谢所有的反馈,因为我真的想在编码方面变得更好。
最佳答案
全局函数必须具有比调用它的地方更高的声明代码。只需重新定位该功能即可。函数 game_menu
必须位于函数 play_one_rps
下面。其他功能相同。
关于python - NameError:在封闭范围内赋值之前引用了自由变量 'player_one_rps',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53553128/
这是我正在调试的函数: boolean adin_memory(char* buffer, int size_chunck, int end_flag){ global_buffer = my
我正在尝试为具有自由 float 底座的机器人计算末端执行器空间速度雅可比行列式。由于自由 float 基数,雅可比应该包含一个基数组件和一个操纵器注释(参见 https://spart.readth
procedure FreeListObjects( l : TStrings); var i : integer; BEGIN FOR i := 0 TO l.Count -1 DO BEG
我正在探索 Haskell 中的选项,这些选项可以让我将业务逻辑与底层系统的技术实现分开。例如,在 Web 服务器的上下文中,将 Web 服务器处理其接收的信息的方式与其读取和写入数据库的方式分开。要
我的目标是使用来自 ActiveMQ 的 WebSphere Liberty Appserver(完整的 Java EE 标准)使用消息。不幸的是,我不知道如何配置 WebSphere Liberty
我以这种方式分配了一个非方阵,但我不确定我是否正确使用了释放 float **matrix_alloc(int m /* rows */, int n /* columns */) { int
我在阅读 refuting the notion 之后的第 13.5 节内置运算符不参与重载决议,并注意到没有关于 operator->* 的部分。它只是一个通用的二元运算符。 它的兄弟operato
我正在尝试使用 Libelf 库来获取有关某些 elf 文件的一些信息。但我不断收到这些“对 [...] 的 undefined reference ”。我从 synaptic 安装了 libelf(
我有创建动态结构数组的波纹管代码。 #include #include #include typedef struct { int flag; char* ip; } ip_mo
我是 StackOverflow 的新人。我现在正在学习C指针。 这是我的代码: #include #include int alloc(int* p){ p = (int*) mallo
我是 StackOverflow 的新人。我现在正在学习C指针。 这是我的代码: #include #include int alloc(int* p){ p = (int*) mallo
我正在用 C 编写一个程序,我需要读入一个文件并打印出每个至少 4 个字符长的字符串。我在分配要使用的内存时遇到问题。字符串可以任意长。我试图将缓冲区分配给文件的大小,然后在最后释放它,但我显然错过了
我尝试用 C 语言编写 ls 命令,但 -R 选项有问题。 输出: /Applications/Atom.app/Contents/Resources/app/apm/node_modules/es5
我正在编写一个 shell,但在执行内存检查时遇到问题,因为 valgrind 无法正常运行。 我遇到了这样的错误(我自己杀死了它): ==19703== Memcheck, a memory err
我有这样一段代码: void *write_parallel(void *num_for_chunk) { struct rusage *sum = (struct rusage*) mall
当使用包含 200-300 个整数(以空格分隔)的输入 .txt 文件运行此代码时,我在使用 fprintf 语句的 for 循环之前收到错误。 我不确定 qsort 是否导致了此错误或为什么会发生此
我试图告诉 Websphere Liberty 我的 log4j2.xml 文件在哪里,但它不起作用。 在我的文件 jvm.options 中,我配置: -Dlog4j.configurationFi
从 websphere liberty 16 迁移到 19.0.0.1 我遇到以下异常:运行存储过程后关闭连接,出现以下异常: EJB threw an unexpected (non-declare
当对大小为 210*8 的种子数据集运行此代码时,我在预测函数中的 qsort() 行之后收到错误。它不在 qsort() 之后执行。 我不确定 qsort 是否导致了此错误或为什么会发生此错误,但如
这个问题已经有答案了: Facing an error "*** glibc detected *** free(): invalid next size (fast)" (2 个回答) 已关闭 9
我是一名优秀的程序员,十分优秀!