石头剪刀布游戏。定义术语时遇到问题

这是迄今为止我制作的最大的程序,我的问题是我的程序无法运行,因为未定义变量 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


  

米琪卡哇伊
浏览 177回答 3
3回答

慕容708150

您应该修改 cpu_choice 函数以返回一个值。此外,还应删除您的 return 语句,如其旁边的评论中所述:def cpu_choice(): #This function is for the computer to get a option.&nbsp; &nbsp; list_option = [1 , 2, 3]&nbsp; &nbsp; cpu = random.choice(list_option)&nbsp; &nbsp; #return(random.choice(list_option)) #This returns a number, but in your compare code you are comparing strings, so take this line out&nbsp; &nbsp; if cpu == 1:&nbsp; &nbsp; &nbsp; &nbsp; cpu = "Rock"&nbsp; &nbsp; if cpu == 2:&nbsp; &nbsp; &nbsp; &nbsp; cpu = 'Paper'&nbsp; &nbsp; if cpu == 3:&nbsp; &nbsp; &nbsp; &nbsp; cpu = 'Scissor'&nbsp; &nbsp; return cpu在主函数中,您可以将另一个名为 cpu 的变量设置为名为 cpu_choice 的函数的返回值def main(cpu,get): #Executes the programs and checks to see if the input is valid.&nbsp; &nbsp; print('Rock = 1')&nbsp; &nbsp; print('Paper = 2')&nbsp; &nbsp; print('Scissor = 3')&nbsp; &nbsp; again = 'y'&nbsp; &nbsp; while get < 1:&nbsp; &nbsp; &nbsp; get = int(input('Enter a valid number.'))&nbsp; &nbsp; while get > 3:&nbsp; &nbsp; &nbsp; get= int(input('Enter a valid number.'))&nbsp; &nbsp; if get == 1:&nbsp; &nbsp; &nbsp; &nbsp; get = "Rock"&nbsp; &nbsp; if get == 2:&nbsp; &nbsp; &nbsp; &nbsp; get = 'Paper'&nbsp; &nbsp; if get == 3:&nbsp; &nbsp; &nbsp; &nbsp; get = 'Scissor'&nbsp; &nbsp; cpu = cpu_choice()&nbsp; &nbsp; compare(cpu,get)

偶然的你

您的 cpu_choice 应如下所示:def cpu_choice():#This function is for the computer to get a option.&nbsp; &nbsp; list_option = [1 , 2, 3]&nbsp; &nbsp; cpu = random.choice(list_option)&nbsp; &nbsp; if cpu == 1:&nbsp; &nbsp; &nbsp; &nbsp; cpu = "Rock"&nbsp; &nbsp; if cpu == 2:&nbsp; &nbsp; &nbsp; &nbsp; cpu = 'Paper'&nbsp; &nbsp; if cpu == 3:&nbsp; &nbsp; &nbsp; &nbsp; cpu = 'Scissor'&nbsp; &nbsp; return cpu这是因为 return 将退出函数,因此函数中 return 语句后面的任何代码将永远不会被执行。您的主要功能应如下所示:def main(cpu,get):# Executes the programs and checks to see if the input is valid.&nbsp; &nbsp; print('Rock = 1')&nbsp; &nbsp; print('Paper = 2')&nbsp; &nbsp; print('Scissor = 3')&nbsp; &nbsp; again = 'y'&nbsp; &nbsp; while get < 1:&nbsp; &nbsp; &nbsp; get = int(input('Enter a valid number.'))&nbsp; &nbsp; while get > 3:&nbsp; &nbsp; &nbsp; get= int(input('Enter a valid number.'))&nbsp; &nbsp; if get == 1:&nbsp; &nbsp; &nbsp; &nbsp; get = "Rock"&nbsp; &nbsp; if get == 2:&nbsp; &nbsp; &nbsp; &nbsp; get = 'Paper'&nbsp; &nbsp; if get == 3:&nbsp; &nbsp; &nbsp; &nbsp; get = 'Scissor'&nbsp; &nbsp; 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'))你的函数声明之后。这是一种常见的做法,可以让您更轻松地理解您的代码。质量优化Pythonrandom可以从列表中选择一个随机元素:Or's 可elif用于在您赢时为 1,如果您输了则为 1。考虑到您main()从内部调用compare()最好main()没有参数,而是在主函数中获取get和cpu一个while语句可以有多个比较。优化后的代码如下所示:&nbsp; &nbsp; import random&nbsp; &nbsp; def cpu_choice():&nbsp; &nbsp; &nbsp; &nbsp; list_option = ["Rock" , "Paper", "Scissor"]&nbsp; &nbsp; &nbsp; &nbsp; cpu = random.choice(list_option)&nbsp; &nbsp; &nbsp; &nbsp; return cpu&nbsp; &nbsp; def compare(cpu,get):&nbsp; &nbsp; &nbsp; &nbsp; if get == cpu:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('Its a tie!')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; again = input('Enter Y or y to play again. Enter N or n to quit.')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if again == 'y' or again == 'Y':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; main()&nbsp; &nbsp; &nbsp; &nbsp; elif cpu == 'Rock' and get == 'Scissor' or cpu == 'Paper' and get == 'Rock' or cpu == 'Scissor' and get == 'Paper':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('You lose.')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; again = input('Enter Y or y to play again. Enter N or n to quit.')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if again == 'y' or again == 'Y':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; main()&nbsp; &nbsp; &nbsp; &nbsp; elif cpu == 'Rock' and get == 'Paper' or cpu == 'Paper' and get == 'Scissor' or cpu == 'Scissor' and get == 'Rock':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('You win!')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; again = input('Enter Y or y to play again. Enter N or n to quit.')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if again == 'y' or again == 'Y':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; main()&nbsp; &nbsp; def main():&nbsp; &nbsp; &nbsp; &nbsp; print('Rock = 1')&nbsp; &nbsp; &nbsp; &nbsp; print('Paper = 2')&nbsp; &nbsp; &nbsp; &nbsp; print('Scissor = 3')&nbsp; &nbsp; &nbsp; &nbsp; get = int(input('Enter a number 1 to 3 as your choice.\n'))&nbsp; &nbsp; &nbsp; &nbsp; cpu = cpu_choice()&nbsp; &nbsp; &nbsp; &nbsp; while not 4 > get > 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get = int(input('Enter a valid number.'))&nbsp; &nbsp; &nbsp; &nbsp; if get == 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get = "Rock"&nbsp; &nbsp; &nbsp; &nbsp; if get == 2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get = 'Paper'&nbsp; &nbsp; &nbsp; &nbsp; if get == 3:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get = 'Scissor'&nbsp; &nbsp; &nbsp; &nbsp; compare(cpu,get)&nbsp; &nbsp; main()

繁华开满天机

在底部,您使用参数“cpu”(未定义)和“get”(由顶部的用户输入定义)调用 main。该程序接受 1 个输入并打印一个输出 - 您不需要将 cpu 参数提供给 main,因为它是从 cpu_choice 函数返回的内容生成的。只需将其作为参数删除并在调用 compare 之前写入 cpu = cpu_choice() ,并让 cpu_choice() 返回 cpu 值。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python