如何将变量带入不同的函数

时间:2019-04-15 06:45:32

标签: python-3.x

首先,为这个沉重,直截了当的代码感到抱歉。 我知道那里有一些更好,更吸引人的代码,但我决定自己编写。我知道还有很大的进步空间,但请耐心等待。

除分数部分外,几乎所有我的代码都能正常工作。

基本上这是一个剪刀石头布游戏,其中记录了玩家,计算机和游戏总数的获胜次数。

我只能将分数传递给另一个函数。有人可以让我知道为什么我在重复游戏功能中选择“ 1-得分”时会收到此错误

Traceback (most recent call last):
  File "forweb.py", line 123, in <module>
    main_menu()
  File "forweb.py", line 27, in main_menu
    play_game()
  File "forweb.py", line 95, in play_game
    return player, computer, total, repeat_game()
  File "forweb.py", line 112, in repeat_game
    play_game()
  File "forweb.py", line 47, in play_game
    play_game()
  File "forweb.py", line 95, in play_game
    return player, computer, total, repeat_game()
  File "forweb.py", line 107, in repeat_game
    print("Total Game: "+ play_game(total))
NameError: name 'total' is not defined

注意:因为它在另一个python文件中,请忽略“从game_text导入game_information”

import random
import sys
import os
import time
from game_text import game_information
os.system('clear')
name = input("Please write your name: ")

def main_menu():

  menu_selection_word = input("1-Help, 2-Play, 3-Quit \n"))
  try:
    menu_selection_int = int(menu_selection_word)
    print("you have selected: ", menu_selection_int)
  except ValueError:
    print(" Invalid selection")
    main_menu()

  if menu_selection_int == 1:
    os.system('clear')
    game_information()
    main_menu()
  elif menu_selection_int == 2:
    play_game()
  elif menu_selection_int == 3:
    game_quit()
  else:
    print("Invaild selection \n")
    main_menu()

def play_game(player=0,computer=0,total=0):
  total += 1
  player_selection_input = input("R-Rock, S-Scissors, P-Paper \n")

  if player_selection_input == "R" or player_selection_input == "r":
    print("You have selected Rock")
  elif player_selection_input == "S" or player_selection_input == "s":
    print("You have selected Scissors")
  elif player_selection_input == "P" or player_selection_input == "p":
    print("You have selected Paper")
  else:
    print("Invaild selection \n")
    play_game()

  comp_random = ["R", "r", "S", "s", "P", "p"]
  comp_selection = random.choice(comp_random)

  if comp_selection == "R" or comp_selection == "r":        
    print("Computer Selected: Rock")
  elif comp_selection == "P" or comp_selection == "p":      
    print("Computer Selected: Paper")
  else:     
    print("Computer Selected: Scissors")

  if player_selection_input == "R" or player_selection_input == "r":
    if comp_selection == "S" or comp_selection == "s":
        print("Your Rock crushed computer's scissors! You Won!")
        player += 1
        time.sleep(1.5)
    elif comp_selection == "R" or comp_selection == "r":
        print("It is a tie!")
        time.sleep(1.5)
    else:
        print("Computer's Paper covered your Rock! You Lost!")
        computer += 1
        time.sleep(1.5)
  elif player_selection_input == "S" or player_selection_input == "s":
    if comp_selection == "S" or comp_selection == "s":
        print(" It is a tie!")
        time.sleep(1.5)
    elif comp_selection == "R" or comp_selection == "r":
        print("Computer's Rock crushed your Scissors. You Lost!")
        computer += 1
        time.sleep(1.5)
    else:
        print("Your Scissors cut computer's Paper. You Won!")
        player += 1
        time.sleep(1.5)
  elif player_selection_input == "P" or player_selection_input == "p":
    if comp_selection == "R" or comp_selection == "r":
        print("Your Paper covered computer's Rock. You Won!")
        player += 1
        time.sleep(1.5)
    elif comp_selection == "S" or comp_selection == "s":
        print("Computer's Scissors cut your Paper. You Lost!")
        computer += 1
        time.sleep(1.5)
    else:
        print(" It is a tie!")
        time.sleep(1.5) 
  return player, computer, total, repeat_game()

def repeat_game():  
  repeat_game_selection = input("1-Score, 2-New game, 3-Quit \n")

  try:
    repeat_game_select = int(repeat_game_selection)
  except ValueError:
    print(" Invalid selection")
    repeat_game()
  if repeat_game_select == 1:
    os.system('clear')
    print("Total Game: "+ play_game(total))
    Print("Player Win: "+ play_game(player))
    print("Computer Win: "+ play_game(computer))
  elif repeat_game_select == 2: 
    print("New Game begins \n")
    play_game()
  elif repeat_game_select == 3:
    game_quit()
  else:
    print("Invaild selection \n")
    repeat_game()

def game_quit():
  os.system('clear')
  sys.exit("Thank you for Playing. See you next time!")

main_menu()

1 个答案:

答案 0 :(得分:0)

您想配对

    return player, computer, total, repeat_game()

打这样的电话:

    player, computer, total, repeat_game = play_game()

也就是说,如果公共API返回了四个值,那么您将希望获取这些值。

四个数字开始变得不方便。 您最好定义一个class并存储四个对象属性, 例如self.player