石头剪刀布游戏。定义术语时遇到麻烦

时间:2018-12-11 21:47:39

标签: python

这是到目前为止我制作的最大的程序,我的问题是我的程序无法运行,因为未定义变量cpu。我尝试了不同的方法,但仍然没有。这是我的代码,还是有缩短我的代码的地方吗?我觉得我做了很多重复的话。我怀疑我的问题出在def cpu_choice()中。此外,该程序的对象在完成后似乎还可以提供任何输出。

#This program will execute a Rock,Paper,Scissors Game.
import random
get = int(input('Enter a number 1 to 3 as your choice.\n'))
def cpu_choice():#This function is for the computer to get a option.
    list_option = [1 , 2, 3]
    cpu = random.choice(list_option)
    return(random.choice(list_option))
    if cpu == 1:
        cpu = "Rock"
    if cpu == 2:
        cpu = 'Paper'
    if cpu == 3:
        cpu = 'Scissor'
def compare(cpu,get):
    again = 'y'
    while again == 'y' or again == 'Y':
      if get == cpu:
          print('Its a tie!')
          again = input('Enter Y or y to play again. Enter N or n to quit.')
          if again == 'y' or again == 'Y':
            main(cpu)
          if again == 'n' or 'N':
             again = False
          #Checks to see if it is a tie 
      elif cpu == 'Rock' and get == 'Scissor':
          print('You win!')
          again = input('Enter Y or y to play again. Enter N or n to quit.')
          if again == 'y' or again == 'Y':
             main(cpu)
          if again == 'n' or 'N':
             again = False
             #Compares when CPU picks Rock.
      elif cpu == 'Rock' and get == 'Paper':
          print('You lose.')
          again = input('Enter Y or y to play again. Enter N or n to quit.')
          if again == 'y' or again == 'Y':
             main(cpu)
          if again == 'n' or 'N':
             again = False

      elif cpu == 'Paper' and get == 'Rock':
          print('You win!')
          again = input('Enter Y or y to play again. Enter N or n to quit.')
          if again == 'y' or again == 'Y':
             main(cpu)
          if again == 'n' or 'N':
             again = False
      elif cpu == 'Paper' and get == 'Scissor':
          print('You lose.')
          again = input('Enter Y or y to play again. Enter N or n to quit.')
          if again == 'y' or again == 'Y':
             main(cpu)
          if again == 'n' or 'N':
             again = False
      elif cpu == 'Scissor' and get == 'Paper':
          print('You win!')
    #This will decide the outcome when the computer picks paper.
          again = input('Enter Y or y to play again. Enter N or n to quit.')
          if again == 'y' or again == 'Y':
             main(cpu)
          if again == 'n' or 'N':
             again = False
      elif cpu == 'Scissor' and get == 'Rock':
          print('You lose.')
          again = input('Enter Y or y to play again. Enter N or n to quit.')
          if again == 'y' or again == 'Y':
             main(cpu)
          if again == 'n' or 'N':
             again = False
    #This decides the outcome if the computer picks scissors.


def main(cpu,get):# Executes the programs and checks to see if the input is valid.
    print('Rock = 1')
    print('Paper = 2')
    print('Scissor = 3')
    again = 'y'

    while get < 1:
      get = int(input('Enter a valid number.'))
    while get > 3:
      get= int(input('Enter a valid number.'))
    if get == 1:
        get = "Rock"
    if get == 2:
        get = 'Paper'
    if get == 3:
        get = 'Scissor'
    cpu_choice()
    compare(cpu,get)
main(cpu,get)

4 个答案:

答案 0 :(得分:0)

您应该修改cpu_choice函数以返回值。另外,还应删除您的return语句,如其旁边的注释所述:

def cpu_choice(): #This function is for the computer to get a option.
    list_option = [1 , 2, 3]
    cpu = random.choice(list_option)
    #return(random.choice(list_option)) #This returns a number, but in your compare code you are comparing strings, so take this line out
    if cpu == 1:
        cpu = "Rock"
    if cpu == 2:
        cpu = 'Paper'
    if cpu == 3:
        cpu = 'Scissor'
    return cpu

在主函数中,可以将另一个名为cpu的变量设置为函数cpu_choice的返回值

def main(cpu,get): #Executes the programs and checks to see if the input is valid.
    print('Rock = 1')
    print('Paper = 2')
    print('Scissor = 3')
    again = 'y'

    while get < 1:
      get = int(input('Enter a valid number.'))
    while get > 3:
      get= int(input('Enter a valid number.'))
    if get == 1:
        get = "Rock"
    if get == 2:
        get = 'Paper'
    if get == 3:
        get = 'Scissor'
    cpu = cpu_choice()
    compare(cpu,get)

答案 1 :(得分:0)

在底部,您要使用参数'cpu'(未定义)和'get'(由用户输入在顶部)定义来调用main。该程序有1个输入并输出一个输出-您不需要将cpu参数输入main,因为它是由cpu_choice函数返回的值生成的。只需删除它作为参数,并在调用compare之前编写cpu = cpu_choice(),并让cpu_choice()返回cpu值。

答案 2 :(得分:0)

您的cpu_choice应该如下所示:

def cpu_choice():#This function is for the computer to get a option.
    list_option = [1 , 2, 3]
    cpu = random.choice(list_option)
    if cpu == 1:
        cpu = "Rock"
    if cpu == 2:
        cpu = 'Paper'
    if cpu == 3:
        cpu = 'Scissor'
    return cpu

这是因为return将退出函数,因此函数中return语句后面的任何代码都将永远不会执行。

您的主要功能应如下所示:

def main(cpu,get):# Executes the programs and checks to see if the input is valid.
    print('Rock = 1')
    print('Paper = 2')
    print('Scissor = 3')
    again = 'y'

    while get < 1:
      get = int(input('Enter a valid number.'))
    while get > 3:
      get= int(input('Enter a valid number.'))
    if get == 1:
        get = "Rock"
    if get == 2:
        get = 'Paper'
    if get == 3:
        get = 'Scissor'

    compare(cpu,get)

您无需在主函数中声明cpu,因为您已经将cpu传递给了主函数。

除了功能之外,您还需要:

get = int(input('Enter a number 1 to 3 as your choice.\n'))
cpu = cpu_choice()
main(cpu,get)

现在您的main函数具有所需的所有参数。注意我在函数声明后放置了get = int(input('Enter a number 1 to 3 as your choice.\n'))。这是使您更容易理解代码的常见做法。

质量优化

  1. Python random可以从列表中选择一个随机元素:

  2. Or可用来赢一个elif,如果输则赢1。

  3. 考虑到您是从main()内部调用compare()的,因此main()最好没有参数,而要获取getcpu主要功能

  4. 一个while语句可以有多个比较。

经过优化的代码如下:

    import random

    def cpu_choice():
        list_option = ["Rock" , "Paper", "Scissor"]
        cpu = random.choice(list_option)
        return cpu

    def compare(cpu,get):
        if get == cpu:
            print('Its a tie!')
            again = input('Enter Y or y to play again. Enter N or n to quit.')
            if again == 'y' or again == 'Y':
                main()
        elif cpu == 'Rock' and get == 'Scissor' or cpu == 'Paper' and get == 'Rock' or cpu == 'Scissor' and get == 'Paper':
            print('You lose.')
            again = input('Enter Y or y to play again. Enter N or n to quit.')
            if again == 'y' or again == 'Y':
                main()
        elif cpu == 'Rock' and get == 'Paper' or cpu == 'Paper' and get == 'Scissor' or cpu == 'Scissor' and get == 'Rock':
            print('You win!')
            again = input('Enter Y or y to play again. Enter N or n to quit.')
            if again == 'y' or again == 'Y':
                main()

    def main():
        print('Rock = 1')
        print('Paper = 2')
        print('Scissor = 3')
        get = int(input('Enter a number 1 to 3 as your choice.\n'))
        cpu = cpu_choice()

        while not 4 > get > 0:
          get = int(input('Enter a valid number.'))

        if get == 1:
            get = "Rock"
        if get == 2:
            get = 'Paper'
        if get == 3:
            get = 'Scissor'

        compare(cpu,get)

    main()

答案 3 :(得分:0)

进行了一些更正后可以正常工作:(阅读评论)

import random

def cpu_choice():
    list_option = [1 , 2, 3]
    cpu = random.choice(list_option)
    if cpu == 1:
        cpu = "Rock"
    if cpu == 2:
        cpu = 'Paper'
    if cpu == 3:
        cpu = 'Scissor'
    return cpu   # the return goes here 

def compare(cpu,get):
    # you are using too many checks for again
    # I'm moving the again checks out of here
    # Moreover, all the results (win, lose) where wrong if you are 'get' 
    print("You -> " + get + "  -  " + cpu + " <- CPU")
    if get == cpu:
        print('Its a tie!')
    elif cpu == 'Rock' and get == 'Scissor':
        print('You lose!')
    elif cpu == 'Rock' and get == 'Paper':
        print('You win.')
    elif cpu == 'Paper' and get == 'Rock':
        print('You lose!')
    elif cpu == 'Paper' and get == 'Scissor':
        print('You win.')
    elif cpu == 'Scissor' and get == 'Paper':
        print('You lose!')
    elif cpu == 'Scissor' and get == 'Rock':
        print('You win.')

def game():   # Don't call it main please, and no need for the arguments

    print('Rock = 1')
    print('Paper = 2')
    print('Scissor = 3')

    get = int(input('Enter a number 1 to 3 as your choice.\n'))

    while (get < 1) or (get > 3):   # No need for two while loops
        get = int(input('Enter a valid number.'))

    if get == 1:
        get = "Rock"
    if get == 2:
        get = 'Paper'
    if get == 3:
        get = 'Scissor'

    # Or you can use this
    # symbols = ["Rock","Paper","Scissor"]
    # get = symbols[get-1]    # it's better if you don't call both variables 'get'

    cpu = cpu_choice()  # you need to assign a value to cpu here
    compare(cpu,get)

    # I put the again code here
    again = input('Enter Y or y to play again. Enter N or n to quit.')
    if again in ['y','Y']:
        game()
    else:
        print("Bye!")

if __name__ == "__main__":   # This is the main
    game()