Question: I`m trying to finish a yathzee scenario in Python where I combine the work so far into a def_yathzee where; - throw 5 dice -
I`m trying to finish a yathzee scenario in Python where I combine the work so far into a def_yathzee where;
- throw 5 dice
- the function yathzee should choose a value to save(1-6) based on the value displayed the most. Reroll remaining dices until yathzee
- display how many rolls was needed to return yathzee
how i want the in/output to be:
In: yatzy(5)
[6, 3, 1, 2, 1]
[4, 6, 1, 4, 1]
[4, 3, 2, 4, 4]
[4, 5, 1, 4, 4]
[4, 3, 6, 4, 4]
[4, 5, 4, 4, 4]
[4, 4, 4, 4, 4]
Out: 7
Would appreciate some guidance/text explaining the steps. As you can see, I`m new to python and programming in general.
So far I got:
#Roll import random
def throw(dice=1, sides=6): result = [] for i in range(dice): numbers = random.randint(1,sides) result.append(numbers) return result
#how many of a given value def find_amount(dice_throw, lst_print = 0): lst_sorted = [] for i in range(6): lst_sorted.append(0) for i in range(len(dice_throw)): for y in range(1,7): if dice_throw[i] == y: lst_sorted[y-1] += 1 if lst_print == 1: print(lst_sorted) return lst_sorted
#most displayed value
def find_most(most, lst_print = 0): if lst_print == 1: print(most) highest = [] highest.append(1) current = most[1] for i in range(1, len(most)): nxt = most[i] if nxt > current: current = nxt highest.clear() highest.append(i+1) elif nxt == current: highest.append(i+1) out = str(highest[0]) if len(highest) > 0: for i in range(1, len(highest)): out += ", " + str(highest[i]) if lst_print == 1: print(str(out)) return highest
#save value for next throw
def new_throw(throw, save): for i in range(len(throw)): if throw[i] != save: throw[i] = random.randint(1,6) return throw
def main(): dice1 = throw(5, 6) print ("Dice1 : ", dice1) dice2 = new_throw(dice1, 6) print("Dice2 :", dice2)
main()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
