0

Trying to achieve exactly the same as on the picture but without None

def game():
    while True:
        try:
            number_of_dices = int(input('Please select how many dices do you want to use: '))
        except ValueError:
            print('Please enter a number')
            continue
        break

    while True:
        try:
            s = int(input('How many sides on your dice? '))
        except ValueError:
            print('Number of sides must be a number.')
            continue
        break

    rolls = []
    def dice_roll(number_of_dices):
        for i in range(number_of_dices):
            dice_roll = random.randint(1, s)
            rolls.append(dice_roll)
            print(dice_roll, end=' ')


    print(dice_roll(number_of_dices))
    print('\nTotal number is: ',sum(rolls))

while True:
    game()
    restart = input('Do you want to restart? Y/N ')
    if restart == 'N' or 'n':
        break
    elif restart == 'Y' or 'y':
        continue

I am doing a little terminal game. I am new to Python (2 weeks) and got stuck with None at the end of print. Now, I know it's because my function doesn't return so I get None. I can remove print and type return there but then it won't print a result from each particular dice and that's a thing I want to keep, so the end Player can see what number was on each dice and a total number. Just have no idea how to do it.

Trying to achieve exactly the same as on the picture but without None.

Jakub
  • 303
  • 2
  • 8
  • 2
    your function is not intended correctly. do you mind fixing it. Also use `return` if you want to return a value from the function back to the calling statement. If you want to print each one, just do a print. Once you exit the loop, you can return `rolls` – Joe Ferndz Apr 11 '21 at 18:10
  • 1
    what is `s` here? is it a global variable that's referenced inside this function? Also what is `rolls`? is that something coming from outside? The code does not have enough information for us to provide a clear answer for you – Joe Ferndz Apr 11 '21 at 18:12
  • s is for a number of sides on the dice. so you can pick. some of the new games have weird dices with like God know how many sides. At the minute I am adding repeat function to its so it's all wrong in that bit btw but I am working this out now. Just fixed the code in my post (sorry that was my first one) and updated it with the rest of the code. – Jakub Apr 11 '21 at 18:20
  • replace this statement `print(dice_roll(number_of_dices))` with `dice_roll(number_of_dices)`. The function is not returning anything. So you are getting the extra `None` with the print statement. That should resolve your problem – Joe Ferndz Apr 11 '21 at 18:37
  • Thank you very much for your help. Now I can go back to working on the restart function. – Jakub Apr 11 '21 at 19:06

1 Answers1

1

What about:

# rolls = []   remove this one

def dice_roll(number_of_dices):
      _rolls = []
      for i in range(number_of_dices):
         dice_roll = random.randint(1, s)
         _rolls.append(dice_roll)
         print(dice_roll, end=' ')
      return _rolls


rolls = dice_roll(number_of_dices)
print('\nTotal number is: ',sum(rolls))

enter image description here

Martí
  • 571
  • 6
  • 17
  • returns only a value for 1 dice even if I've picked 4 dices. – Jakub Apr 11 '21 at 18:26
  • in the code I posted, ``len(_rolls) == number_of_dices`` at the time it returns. Do you need to return only 2 values despite ``number_of_dices``? – Martí Apr 11 '21 at 18:28
  • preferably need to return a value for each dice – Jakub Apr 11 '21 at 18:31
  • The returned value you put in ``rolls`` will be a list, where you can access each value independently (e.g. ``rolls[0], rolls[1]``), like ``[print(result) for result in rolls]`` will print the result of each dice. Writing a program that returns a value for each result will require that you call the function that many times, and would need to know ``number_of_dices`` beforehand, it feels quite against the flexibility that programming provides per se. – Martí Apr 11 '21 at 18:34
  • When you provide a response, try and explain the changes being made to the code. That way OP and others who come to this thread years from now will understand what was done. Code only solutions do not provide value to anyone. – Joe Ferndz Apr 11 '21 at 18:41
  • Thank you very much for your help. Alterations you did actually removed None and the program is working as I wanted. . Now I can go back to working on the restart function. – Jakub Apr 11 '21 at 19:02