As a way to learn python, I am building Yahtzee.py.
After the first roll, I would like the user to decide what to keep or what to reroll (up to 3 times).
So as to avoid writing a code for every scenario how can I allow user to select which dice to reroll. ie( keep die 1 or keep dice 2, 3,5)
Here is the code I have so far:
import random
rollCount=1
roll1 = random.randint(1,6)
roll2 = random.randint(1,6)
roll3 = random.randint(1,6)
roll4 = random.randint(1,6)
roll5 = random.randint(1,6)
def rollAll():
roll1
roll2
roll3
roll4
roll5
def printAll():
print("roll 1:",roll1,"\nroll 2:",roll2,"\nroll 3:",roll3,"\nroll 4:",roll4,"\nroll 5:",roll5)
def printRoll():
print("press any key to roll dice")
input()
str(printAll())
print("Would you like to roll again?\nroll all, roll 1, roll 2 , roll 3, roll 4 , roll 5")
rollAgain = input()
if rollAgain== "roll all":
rollCount=2
rollAll()
str(printAll())
Welcome to Python! :)
As your post indicates that this is a learning exercise, I will offer advice for objectives you should try to reach, rather than writing and pasting your code for you.
Your program needs to remember the roll for each die. One of many mechanisms for this could be:
outcomes = []
for i in range(0,5):
outcomes.append(random.randint(1,6))
Given the above, you now have programmatic memory of each outcome. Each of the random.randint() outcomes are now kept in a list, which you can access by element later in your program, depending on what the user chooses to keep or reroll. Remember that the user's perception of "die one" is actually element zero in your list, due to the way indices are numbered.
There are over a dozen ways to approach the remainder of your program, but this should be a good starter, for you. You could even explore list comprehensions to improve on the sample I provide, above.
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.