简体   繁体   中英

How can I get new results from randint while in a loop?

My code thus far:

from random import randint

Dice1 = randint(1,6)
Dice2 = randint(1,6)
Dice3 = randint(1,6)

DiceRoll2 = Dice1 + Dice2
DiceRoll3 = Dice1 + Dice2 + Dice3

class Item(object):
    def __init__(self, name, value, desc):
        self.name = name
        self.value = value
        self.desc = desc

sword = Item("Sword", 2, "A regular sword.")

class Monster(object):
    def __init__(self, name, health, attack):
        self.name = name
        self.health = health
        self.attack = attack

monster = Monster("Monster", 50, DiceRoll2)

Damage = DiceRoll3 + sword.value
NewHealth = monster.health - Damage

print("You see a monster!")

while True:
    action = input("? ").lower().split()

    if action[0] == "attack":
        print("You swing your", sword.name, "for", Damage, "damage!")
        print("The", monster.name, "is now at", NewHealth, "HP!")

    elif action[0] == "exit":
        break

The intension is that with every time you enter "attack" you get a random result of DiceRoll3 (three random numbers from 1 to 6, that three times), add the value of the sword and substract that from the monster's starting health. This goes well until I enter "attack" a second time, which results in the same damage and the same reduced health being printed instead of using a new value. How can I properly do this?

Put your dice-rolling into a separate function and calculate Damage and NewHealth inside your loop. (Also, update your Monster's health. :))

from random import randint

def dice_roll(times):
  sum = 0
  for i in range(times):
    sum += randint(1, 6)
  return sum

class Item(object):
  def __init__(self, name, value, desc):
    self.name = name
    self.value = value
    self.desc = desc

sword = Item("Sword", 2, "A regular sword.")

class Monster(object):
  def __init__(self, name, health, attack):
    self.name = name
    self.health = health
    self.attack = attack

monster = Monster("Monster", 50, dice_roll(2))

print("You see a monster!")

while True:
  action = input("? ").lower().split()

  if action[0] == "attack":
    Damage = dice_roll(3) + sword.value
    NewHealth = max(0, monster.health - Damage)   #prevent a negative health value
    monster.health = NewHealth                    #save the new health value
    print("You swing your", sword.name, "for", Damage, "damage!")
    print("The", monster.name, "is now at", NewHealth, "HP!")

  elif action[0] == "exit":
    break

  if monster.health < 1:
    print("You win!")
    break

You need to make the call to randint everytime. Below is one way to do it

def Dice1(): return  randint(1,6)
def Dice2(): return  randint(1,6)
def Dice3(): return  randint(1,6)

This isn't great though because of all the repeated code. Here is a better way

class Dice(object):
    def roll_dice(self):
        return randint(1,6)

Dice1 = Dice()
Dice2 = Dice()
Dice3 = Dice()

Now in your code where ever you call Dice1 , Dice2 or Dice3 ; instead call Dice1.roll_dice() , Dice2.roll_dice() , Dice3.roll_dice() . This abstract away the dice implementation and you are free to change it later without having to change your code.

If you need have dice with different number of faces, you only need to change your dice class

class Dice(object):
    def __init__ (self, num_faces=6):
        self.faces = num_faces

    def roll_dice(self):
        return randint(1,self.faces)

Dice1 = Dice() # 6 faced die
Dice2 = Dice(20) # 20 faced die

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM