user836420
user836420

Reputation: 41

Add two outcomes of a dice together python

I'm an absolute beginner to python but I really want to challenge myself. I want to create a game where a dice is rolled for each player (for now, two players) 2 times. The outcomes of the dice is added and if its even 10 points are added and if its odd, 5 points are subtracted. The players can play up to 5 rounds. So far, I've kept the code within a while loop and imported random to "roll" the dice but I don't know how to add the random outcomes together.

My code might be completely wrong but I would love some help and advice regarding how to fix it (this is done on python 3)

My code:

person1_name = input("What is your name: ")
person2_name = input("What is your name: ")

import random
number = random.randint(1,6)
rounds = "yes"
while rounds == "yes":
    print(person1_name, "- 1st roll = ",number, " and 2nd roll = ",number)
    total_1 = number + number
    if total_1 % 2 == 0:
        total_1 = total_1 + 10
        print(person1_name," has ",total_1, "points")
    else:
        total_1 = total_1 - 5
        print(person1_name, " has ",total_1, "points")
    print(person2_name, "- 1st roll = ",number, "and 2nd roll = ",number)
    total_2 = number + number
    if total_2 % 2 == 0:
        total_2 = total_2 + 10
        print(person2_name," has ",total_2, "points")
    else:
        total_2 = total_2 - 5
        print(person2_name," has ",total_2, "points")
    rounds = input("Do you want to play again (yes/no): ")

Upvotes: 2

Views: 2726

Answers (4)

Miraj50
Miraj50

Reputation: 4407

As stated, you need to call random.randint() to generate random numbers inside the loop. For keeping count of number of rounds, use another variable. Try this :

import random

person1_name = input("What is your name: ")
person2_name = input("What is your name: ")
rounds = "yes"
person1_tot, person2_tot = 0,0 ##
numround = 1 # Number of rounds
while rounds == "yes" and numround<=5:
    number1 = random.randint(1,6)
    number2 = random.randint(1,6)
    print(person1_name, "- 1st roll = ",number1, " and 2nd roll = ",number2)
    total_1 = number1 + number2
    if total_1 % 2 == 0:
        total_1 = total_1 + 10
        print(person1_name," has ",total_1, "points")
    else:
        total_1 = total_1 - 5
        print(person1_name, " has ",total_1, "points")
    person1_tot += total_1 ##
    print(person1_name, " : ", person1_tot) ##
    number1 = random.randint(1,6)
    number2 = random.randint(1,6)
    print(person2_name, "- 1st roll = ",number1, "and 2nd roll = ",number2)
    total_2 = number1 + number2
    if total_2 % 2 == 0:
        total_2 = total_2 + 10
        print(person2_name," has ",total_2, "points")
    else:
        total_2 = total_2 - 5
        print(person2_name," has ",total_2, "points")
    person2_tot += total_2 ##
    print(person2_name, " : ", person2_tot) ##
    numround+=1
    rounds = input("Do you want to play again (yes/no): ")

Upvotes: 1

Tilak Putta
Tilak Putta

Reputation: 778

Here you go with number of dice and number of players:

number_of_players = int(raw_input("Number of players: "))
number_of_dice = int(raw_input("Number of dice: "))
player_names = []
player_points = []
for i in range(number_of_players):
    name = raw_input("What is your name: ")
    player_names.append(name)
    player_points.append(0)

import random

def roll(number_of_dice=2):
    total = 0
    rolls = []
    for i in range(number_of_dice):
        dice = random.randint(1,6)
        print("dice "+str(i+1)+": "+str(dice))
        total += dice
        rolls.append(dice)
    return rolls,total
rounds = "yes"
while rounds == "yes":
    for i in range(number_of_players):

        print("rolling dice for "+player_names[i]+"....")
        rolls,total = roll(number_of_dice)
        if total % 2 == 0:
            player_points[i] += 10
        else:
            player_points[i] -= 5
        print(player_names[i]+" has "+str(player_points[i])+" points")
    rounds = raw_input("Do you want to play again (yes/no): ")

Upvotes: -1

Drumming Ace
Drumming Ace

Reputation: 99

you should pay attention to where you created the random number. In your code it is out of the loop meaning through out the whole session you would only produce one random number. I presumed you wanted to use two dies as well so that would mean you need another number variable.

import random

person1_name = input("What is your name: ")
person2_name = input("What is your name: ")

rounds = "yes"
while rounds == "yes":
    number, number2 = random.randint(1,6), random.randint(1,6)
    total_1 = number + number2
    print(person1_name, "- 1st roll = ",number, " and 2nd roll = ",number2)
    if total_1 % 2 == 0:
        total_1 = total_1 + 10
        print(person1_name," has ",total_1, "points")
    else:
        total_1 = total_1 - 5
        print(person1_name, " has ",total_1, "points")
    number, number2 = random.randint(1,6), random.randint(1,6)
    total_2 = number + number2
    print(person2_name, "- 1st roll = ",number, "and 2nd roll = ",number2)
    if total_2 % 2 == 0:
        total_2 = total_2 + 10
        print(person2_name," has ",total_2, "points")
    else:
        total_2 = total_2 - 5
        print(person2_name," has ",total_2, "points")
    rounds = input("Do you want to play again (yes/no): ")    

Upvotes: 0

abc
abc

Reputation: 11939

You may directly get the sum using random.choices().

sum_dices = sum(random.choices(range(1,7),k=2))

or directly check:

if sum(random.choices(range(1,7),k=2)) % 2:
     pass
else:
     pass

Upvotes: 3

Related Questions