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

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

需要代碼讓用戶重復嘗試

需要代碼讓用戶重復嘗試

絕地無雙 2023-05-23 16:02:11
我是編碼新手,所以我正在嘗試創建剪刀石頭布游戲。我幾乎完成了游戲,但是當用戶和計算機輸入相同的數字時,我希望程序重復,直到其中一個玩家獲勝。我怎樣才能做到這一點?任何幫助表示贊賞。這是我的代碼。game = input("Want to play Rock Paper Scissors? (Y/N) ")if game == "Y":  print("1 = Rock, 2 = Paper, 3 = Scissors")  print('')  user = int(input("You have one chance of beating me. Input a number. "))  print('')  import random  computer = random.randint(1,3)  if user == 1 and computer == 1:    print("Must play again! We both played Rock!")  elif user == 1 and computer == 2:    print("You lose! You played Rock and I played Paper!")  elif user == 1 and computer == 3:    print("You win! You played Rock and I played Scissors!")  elif user == 2 and computer == 1:    print("You win! You played Paper and I played Rock!")  elif user == 2 and computer == 2:    print("Must play again! We both played Paper!")  elif user == 2 and computer == 3:    print("You lose! You played Paper and I played Scissors!")  elif user == 3 and computer == 1:    print("You lose! You played Scissors and I played Rock!")  elif user == 3 and computer == 2:    print("You win! You played Scissors and I played Paper!")  elif user == 3 and computer == 3:    print("Must play again! We both played Scissors!")  else:    print("Not a number.")  else:  print("Fine. Bye.")  
查看完整描述

3 回答

?
隔江千里

TA貢獻1906條經驗 獲得超10個贊

(要了解更多 google 引號中的任何內容 :)


使用“while 循環”創建“游戲循環”。我從用 python 制作小游戲中學到了這個技巧。此外,您想學習使用“類”作為邏輯,并且可以使用“OOP 概念”改進代碼。T 代碼已經過測試并且可以工作。


import random


#Create a "function" that meets the requirements of a "game loop"

def gameloop():

    game = input("Want to play Rock Paper Scissors? (Y/N) ")


    

    if game == "Y":


         #Create a "while loop" to host the logic of the game. 

         #Each If statement will enable one "rule" of the game logic.

         #game logic could be redesigned as an "Event".

         #You can add a game "Event System" to your future project backlog


        winner = False

        while not winner:

            print("1 = Rock, 2 = Paper, 3 = Scissors")

            print('')

            user = int(

                input("You have one chance of beating me. Input a number. "))

            print('')


            computer = random.randint(1, 3)


            if user == 1 and computer == 2:

                print("You lose! You played Rock and I played Paper!")

                winner = True


            elif user == 1 and computer == 3:

                print("You win! You played Rock and I played Scissors!")

                winner = True


            elif user == 2 and computer == 1:

                print("You win! You played Paper and I played Rock!")

                winner = True


            elif user == 2 and computer == 3:

                print("You lose! You played Paper and I played Scissors!")

                winner = True


            elif user == 3 and computer == 1:

                print("You lose! You played Scissors and I played Rock!")

                winner = True


            elif user == 3 and computer == 2:

                print("You win! You played Scissors and I played Paper!")

                winner = True


            elif user == 1 and computer == 1:

                print("Must play again! We both played Rock!")


            elif user == 2 and computer == 2:

                print("Must play again! We both played Paper!")


            elif user == 3 and computer == 3:

                print("Must play again! We both played Scissors!")


            else:

                print("Not a number.")


else:

    print("game....over?")

    

gameloop()

我也花時間做了一個 OOP 類示例!它可以通過多種方式進行優化。但是,我希望它能向您展示下一個學習水平!我也希望它能幫助您了解所有您可以在學習時應用的瘋狂設計模式和游戲編程技術。


import random


# we can create a player class to be used as an "interface" as we design the games logic

# this will let us scale the features be build in our game

# in this case i will leave it to you to add Wins to the scoreboard to help you learn


class player:

    # your games requerment wants your players to make a choice from 1-3

    choice = 0

    # your games may requerment a player to be defined as the winner

    win = 0

    # your games may requerment a player to be defined as the losser

    loss = 0



class game:

    # by using classes and OOP we can scale the data and logic of your game

    # here we create instances of the class player and define new objects based on your "requerments"

    # your "requerments" where to have one Computer as a player, and one user as a player

    computer = player()

    user = player()


    # this "function" will create a Scoreboard feature that can be called in the 'game loop' or in a future "event" of the game.

    # Like a "Game Stats stage" at the end of the game

    def Scoreboard(self, computer, user):

        Computer = computer.loss

        User = user.loss

        print("+============= FINAL RESULTS: SCOREBOARD!! ======+ ")

        print(" ")

        print("Computer losses: ", Computer)

        print("Player losses: ", User)

        print(" ")


    # Create a "function" that meets the requirements of a "game loop"

    def main_loop(self, computer, user):

        gameinput = input("Want to play Rock Paper Scissors? (Y/N) ")


        if gameinput == "Y":


           # Create a "while loop" to host the logic of the game.

           # Each If statement will enable one "rule" of the game logic.

           # game logic could be redesigned as an "Event".

           # You can add a game "Event System" to your future project backlog


            winner = False

            while not winner:

                print("1 = Rock, 2 = Paper, 3 = Scissors")

                print('')


                # we create 'Player1' as the user

                Player1 = user

                

                # we change the 'Player1' 'choice' to the user input

                Player1.choice = int(

                    input("You have one chance of beating me. Input a number. "))


                print('')

                

                # we pull in to the game the computer player and call them 'Player1'

                Player2 = computer

                

                # we change the 'Player2' 'choice' to a random number

                Player2.choice = random.randint(1, 3)

            


                if user.choice == 1 and computer.choice == 2:

                    print("You lose! You played Rock and I played Paper!")

                    winner = True

                    user.loss += 1

                elif user.choice == 1 and computer.choice == 3:

                    print("You win! You played Rock and I played Scissors!")

                    winner = True

                    computer.loss += 1

                elif user.choice == 2 and computer.choice == 1:

                    print("You win! You played Paper and I played Rock!")

                    winner = True

                    computer.loss += 1

                elif user.choice == 2 and computer.choice == 3:

                    print("You lose! You played Paper and I played Scissors!")

                    winner = True

                    user.loss += 1

                elif user.choice == 3 and computer.choice == 1:

                    print("You lose! You played Scissors and I played Rock!")

                    winner = True

                    user.loss += 1

                elif user.choice == 3 and computer.choice == 2:

                    print("You win! You played Scissors and I played Paper!")

                    winner = True

                    computer.loss += 1

                elif user.choice == 1 and computer.choice == 1:

                    print("Must play again! We both played Rock!")

                elif user.choice == 2 and computer.choice == 2:

                    print("Must play again! We both played Paper!")

                elif user.choice == 3 and computer.choice == 3:

                    print("Must play again! We both played Scissors!")

                else:

                    print("Not a number.")

            # by returning "self" you call the same 'instances' of game that you will define below

            return self.Scoreboard(user, computer)

        else:

            print("game....over?")



# define Instance of game as "test_game"

test_game = game()


# run game loop


test_game.main_loop()



查看完整回答
反對 回復 2023-05-23
?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

一種方法是使用 while 循環,在滿足條件時中斷。


while True:

    if (condition):

        print("")

        break

    ...

while 語句重復循環,直到滿足其中一個條件。break 語句使程序退出循環并繼續執行下一個可執行語句。


查看完整回答
反對 回復 2023-05-23
?
幕布斯6054654

TA貢獻1876條經驗 獲得超7個贊

您可以將整個 if-elif-block 放在一個 while 循環中,該循環將重復直到您有一個贏家。為了確定是否有贏家,使用布爾變量。


winner = False

while not winner:

    if ...... ## Your if-elif-block


    elif user == 1 and computer == 2:

        print("You lose! You played Rock and I played Paper!")

        winner = True


    ## Your remaining if-elif-block


您只需將命令放在winner=True具有獲勝者條件的命令塊中。因此,循環將繼續,直到您達到這些條件之一。


您還可以選擇使用更高級的獲勝者變量(0 表示平局,1 表示玩家,2 表示計算機)來使用再見消息中的值。


查看完整回答
反對 回復 2023-05-23
  • 3 回答
  • 0 關注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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