亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

石頭剪刀布游戲。定義術語時遇到問題

石頭剪刀布游戲。定義術語時遇到問題

米琪卡哇伊 2021-09-14 17:25:15
這是迄今為止我制作的最大的程序,我的問題是我的程序無法運行,因為未定義變量 cpu。我嘗試了不同的方法,但仍然一無所獲。這是我的代碼,無論如何也可以縮短我的代碼嗎?我覺得我做了很多重復的臺詞。我懷疑我的問題出在 def cpu_choice() 中。此外,該程序在完成后似乎沒有任何輸出。#This program will execute a Rock,Paper,Scissors Game.import randomget = 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  
查看完整描述

3 回答

?
慕容708150

TA貢獻1831條經驗 獲得超4個贊

您應該修改 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)


查看完整回答
反對 回復 2021-09-14
?
偶然的你

TA貢獻1841條經驗 獲得超3個贊

您的 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'))你的函數聲明之后。這是一種常見的做法,可以讓您更輕松地理解您的代碼。


質量優化


Pythonrandom可以從列表中選擇一個隨機元素:


Or's 可elif用于在您贏時為 1,如果您輸了則為 1。


考慮到您main()從內部調用compare()最好main()沒有參數,而是在主函數中獲取get和cpu


一個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()



查看完整回答
反對 回復 2021-09-14
?
繁華開滿天機

TA貢獻1816條經驗 獲得超4個贊

在底部,您使用參數“cpu”(未定義)和“get”(由頂部的用戶輸入定義)調用 main。該程序接受 1 個輸入并打印一個輸出 - 您不需要將 cpu 參數提供給 main,因為它是從 cpu_choice 函數返回的內容生成的。只需將其作為參數刪除并在調用 compare 之前寫入 cpu = cpu_choice() ,并讓 cpu_choice() 返回 cpu 值。


查看完整回答
反對 回復 2021-09-14
  • 3 回答
  • 0 關注
  • 207 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號