gpt4 book ai didi

python - NameError:在封闭范围内赋值之前引用了自由变量 'player_one_rps'

转载 作者:太空宇宙 更新时间:2023-11-03 21:30:35 25 4
gpt4 key购买 nike

我对编码很陌生,我一直在尝试制作一个带有菜单的基于文本的游戏。游戏本身工作正常,但一旦我尝试合并菜单,我收到错误“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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com