当前位置: 首页 > 知识库问答 >
问题:

我如何使我的代码循环的函数战斗()[复制]

奚修伟
2023-03-14
import random
user = input("You reconize this creture. The feared BaLaKe. Its weakness is 'rock', 'paper',''scissors' Are you willing to challenge him in a battle of Rock Paper Scissors? (ENTER)" )
print("ROARRRRRRRRRRRRRRRRRRR!!!!!")

下面是我想在用户健康和龙健康时循环的功能

def fight(userhealth,dragonhealth):
 dragon = random.choice(["r","p","s"])
 user=input("Rock(r), Paper(p) or Scissors(s): ").lower()
 print(dragon)
 if user==dragon:
     print("You both missed your attacks. Bruh ;-;")
 elif is_win(user,dragon):

        dragonhealth-=20 
        print("You hit BaLaKe! 20 DMG Done!!! :) ") 
        print("Your health: "+str(userhealth )+" HP")
        print("BaLaKe\'s health: "+str(dragonhealth )+" HP") 
        
     else: 
      print("Ow! You got hit by BaLake. -20 HP :( ")     
      userhealth-=20 
      print("Your health: "+str(userhealth )+" HP")
      print("BaLaKe\'s health: "+str(dragonhealth )+" HP")
    
    def is_win(player,opponent):
        if (player=="r" and opponent=="s") or (player=="s" and opponent=="p") or (player=="p" and opponent=="r"):
         return True
    
    
    fight(100,100)
       

    

共有3个答案

全飞扬
2023-03-14

>

  • 使用While循环重复游戏直到某人获得0生命。

    我创建了一个函数,当有人生命值为0时终止游戏。看到了吗

    在这里输入代码

    import random
    
    user_health = 100  # used underscore snake case love:)
    dragon_health = 100  # I initialized both variables outside
    
    
    def fight(user_health, dragon_health):  # according to PEP8 I guess. we shouldn't use shadow names. Still,not a big deal
        dragon = random.choice(["r", "p", "s"])
        user = input("Rock(r), Paper(p) or Scissors(s): ").lower()
        print(dragon)
    
        if user == dragon:
            print("You both missed your attacks. Bruh ;-;")
            return user_health, dragon_health
        elif is_win(user, dragon):
    
            dragon_health -= 20
            print("You hit BaLaKe! 20 DMG Done!!! :) ")  #
            print("Your health: " + str(user_health) + " HP")
            print("BaLaKe\'s health: " + str(dragon_health) + " HP")
            return user_health, dragon_health
    
        else:
            print("Ow! You got hit by BaLake. -20 HP :( ")
            user_health -= 20
            print("Your health: " + str(user_health) + " HP")
            print("BaLaKe\'s health: " + str(dragon_health) + " HP")
            return user_health, dragon_health
    
    
    def is_win(player, opponent):
        if (player == "r" and opponent == "s") or (player == "s" and opponent == "p") or (
                player == "p" and opponent == "r"):
            return True
    
    
    # this function terminates game when someone is at 0 HP and prints output
    def game_terminator(user_health, dragon_health):  # I continued with the shadow names to avoid confusion
        if user_health == 0:
            print("Dragon killed you!")
            return False
        elif dragon_health == 0:
            print("Thrashed dragon to dealth! YOU WON!")  # Nerd outputs :)
            return False
        else:
            return True
    
    
    game_is_on = True
    # Use while loop for game to run until someone gets 0 HP
    while game_is_on:  # game_is_on is a boolean variable
        user_health, dragon_health = fight(user_health, dragon_health)
        game_is_on = game_terminator(user_health, dragon_health)
    
      # Thank Me :)
    

  • 崔高远
    2023-03-14

    尝试以下代码:

    import random
    
    user = input("You reconize this creture. The feared BaLaKe. Its weakness is 'rock', 'paper',''scissors' Are you willing to challenge him in a battle of Rock Paper Scissors? (ENTER)" )
    print("ROARRRRRRRRRRRRRRRRRRR!!!!!")
    dragonhealth = 100
    userhealth = 100
    
    def fight(userhealth,dragonhealth,choice):
        dragon = random.choice(["r","p","s"])
        print(dragon)
        if choice==dragon:
            print("You both missed your attacks. Bruh ;-;")
    
        elif is_win(choice,dragon):
            dragonhealth-=20 
            print("You hit BaLaKe! 20 DMG Done!!! :) ") 
            print("Your health: "+str(userhealth )+" HP")
            print("BaLaKe\'s health: "+str(dragonhealth )+" HP") 
            
        else: 
            print("Ow! You got hit by BaLake. -20 HP :( ")     
            userhealth-=20 
            print("Your health: "+str(userhealth )+" HP")
            print("BaLaKe\'s health: "+str(dragonhealth )+" HP")
        return userhealth,dragonhealth
            
    def is_win(player,opponent):
        if (player=="r" and opponent=="s") or (player=="s" and opponent=="p") or (player=="p" and opponent=="r"):
            return True
    
    while (userhealth>0 or dragonhealth>0):
        choice=input("Rock(r), Paper(p) or Scissors(s): ").lower()
        userhealth,dragonhealth = fight(userhealth,dragonhealth,choice)
    
    if userhealth>0:
        print("You won! ")
    elif dragonhealth>0:
        print("You lost! ")
    

    说明:在开始时设置原始运行状况。设置while循环以检查它们中的任何一个的运行状况是否小于0。输/赢时减去并更新生命值。最后宣布获胜!

    仲孙经赋
    2023-03-14

    你想要这个吗?

    import random
    user = input("You recognize this creture. The feared BaLaKe. Its weakness is 'rock', 'paper',''scissors' Are you willing to challenge him in a battle of Rock Paper Scissors? (ENTER)" )
    print("ROARRRRRRRRRRRRRRRRRRR!!!!!")
    def fight(userhealth,dragonhealth):
    
      while(userhealth>0 and dragonhealth>0):
    
        dragon = random.choice(["r","p","s"])
        user=input("Rock(r), Paper(p) or Scissors(s): ").lower()
        print(dragon)
        if user==dragon:
            print("You both missed your attacks. Bruh ;-;")
        elif is_win(user,dragon):
    
                dragonhealth-=20 
                print("You hit BaLaKe! 20 DMG Done!!! :) ") 
                print("Your health: "+str(userhealth )+" HP")
                print("BaLaKe\'s health: "+str(dragonhealth )+" HP") 
                
        else: 
            print("Ow! You got hit by BaLake. -20 HP :( ")     
            userhealth-=20 
            print("Your health: "+str(userhealth )+" HP")
            print("BaLaKe\'s health: "+str(dragonhealth )+" HP")
            
        def is_win(player,opponent):
            if (player=="r" and opponent=="s") or (player=="s" and opponent=="p") or (player=="p" and opponent=="r"):
                return True
    
    
    fight(100,100)
    
     类似资料:
    • 我想在数组上循环,并使我的循环返回函数或一个时间字符串表示名称存在?这是错误类型错误:无法读取未定义的var x=arr[i]的属性“name”。.name;

    • 我试图找出如何简化创建游戏对象的循环。这是我在为如何使这本书更容易阅读而感到困惑之前所做的。 `使用系统。收藏;使用系统。收藏。通用的使用UnityEngine; 公共类游戏对象:单一行为{ }' 感谢您的帮助,并指出了我可以改进这一点的更好方法。

    • 我的代码需要反复提示用户输入一个整数。当用户不再想继续输入数字时,输出用户输入的所有正数之和,然后输出用户输入的所有负数之和。以下是我到目前为止的情况。

    • 我尝试了多种方法来运行它。目的是绘制用户可以点击的圆圈。我无法在JGroup上绘制任何东西。我最初是在扩展JFrame,但意识到我需要扩展JGroup。我正在使用IntelliJ GUI Designer进行布局。我将非常感谢任何帮助。 这是IntelliJ表单设计器文件。 https://pastebin.com/Z3b0PVtZ

    • 我目前的目标是让gradle在我的机器上的Jetty实例中启动我的web应用程序,这样我就可以针对它编写Selenium测试。Gretty插件似乎正在加载,但我还没有找到任何关于如何创建和配置任务的实际说明。 部分问题是Gretty插件的不同版本和版本存在混淆。首先加载它是一个反复试验的过程。 我正在尝试使用Gradle 5.4.1和Gretty 2.3.1,我相信这是当前的版本(目前)。 我有三