简体   繁体   中英

I am trying to make my dice game so that instead of it changing one of numbers of the dice each roll it changes all of them

So the problem is in line 9 through 32. It outputs say "3 0 0" then "3 4 0" then "3 4 6". Instead it should say "3 4 6" then it could be "6 1 2". I know it's to do with the numRolled variable and the way it loops with that but I cannot think where else to put it.

I've tried putting numRolled = 0 at the end of the "for i in range(3)" but that just makes it so that only the first number changes. I've tried making the numbers into a single variable list but I am not really confident with coding in lists so I decided to go with what I have here.

def DiceGame():

  numRolled = 1
  RunScore = 0
  Roll1 = 0
  Roll2 = 0
  Roll3 = 0
  #rolls the 3 dice 100 times
  for x in range(100): 
    numRolled = 0
    #rolls the 3 dice
    for i in range(3):
      score = rdm.randint(1,6)
      numRolled += 1
      #assigns score to each die
      if numRolled == 1:
        Roll1 = score
      if numRolled == 2:
        Roll2 = score
      if numRolled == 3:
        Roll3 = score

      if Roll1 == Roll2 and Roll1 == Roll3:
        RunScore += 100
      else:
        DiceTot = Roll1 + Roll2 + Roll3
      # If Total of Die MOD 2 == 0 then Running score += Dice Total 
      if DiceTot % 2 == 0:
        RunScore += DiceTot
      else:
        RunScore -= DiceTot  
      print(Roll1, Roll2, Roll3)
      print(RunScore)

As I said above, it outputs say "3 0 0" then "3 4 0" then "3 4 6". Instead it should say "3 4 6" then it could be "6 1 2"

change this

def game: 
    forlopp:
        forloop:
            print()
            print()

to this

def game: 
    forlopp:
        forloop:
        print()
        print()

Move everything not related to individual dice rolls out of the first loop:

  for x in range(100): 
    numRolled = 0
    #rolls the 3 dice
    for i in range(3):
      score = rdm.randint(1,6)
      numRolled += 1
      #assigns score to each die
      if numRolled == 1:
        Roll1 = score
      if numRolled == 2:
        Roll2 = score
      if numRolled == 3:
        Roll3 = score

    ####
    # Here the code below has been un-indented and removed from the range(3) loop
    ####

    if Roll1 == Roll2 and Roll1 == Roll3:
      RunScore += 100
    else:
      DiceTot = Roll1 + Roll2 + Roll3


      #####
      # Note: I have indented the following block to put it inside
      #       this "else" clause, so that it can use "DiceTot" reliably.
      #####

      # If Total of Die MOD 2 == 0 then Running score += Dice Total 
      if DiceTot % 2 == 0:
        RunScore += DiceTot
      else:
        RunScore -= DiceTot  
    print(Roll1, Roll2, Roll3)
    print(RunScore)

That should fix the code.

However, this would be easier using lists. You can generate a list of 3 dice rolls like this:

rolls = []
for _ in range(3):
  rolls.append(rdm.randint(1,6))

It can also be written as a list comprehension instead:

rolls = [rdm.randint(1,6) for _ in range(3)]

Whichever you do, you can then generate your statistics more easily:

if all(roll == rolls[0] for roll in rolls):
  RunScore += 100
else:
  DiceTot = sum(rolls)
  if DiceTot % 2 == 0:
    RunScore += DiceTot
  else:
    RunScore -= DiceTot

And you can print using the join function:

print(" ".join(rolls))

Using a list like this would allow you to get rid of the 3 dice roll variables and allow you to change the number of dice rolls on a whim without rewriting anything.

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