1
\$\begingroup\$

I feel as if I did not write it efficiently. Any suggestions on slimming? I had trouble when player 2 would guess the number the first try, it would say player 2 won but keep continuing to the re-roll loop, hence the sys.exit().

import random
import time
import sys

diceNumber = random.randint(1,6)



print("lets play dice of destiny")

play1 = input("Player 1 name?")
play2 = input("Player 2 name?")
prize = input("What does the loser have to do?")

print("Hi " + play1 + " & " + play2 + ", let" + "'" + "s roll the dice 
of destiny.")

play1Num = input(play1 + " choose a number 1-6.")
play2Num = input(play2 + " choose a number 1-6.")

play1Num = int(play1Num)
play2Num = int(play2Num)

if play1Num == diceNumber:
    print("The Dice rolled...")
    print(diceNumber)
    print(play1.upper() + " WINS!")
    print(play2 + " must: " + prize)
    sys.exit()

if play2Num == diceNumber:
    print("The Dice rolled...")
    print(diceNumber)
    print(play2.upper() + " WINS!")
    print(play1 + " must: " + prize)
    sys.exit()

while diceNumber != play1Num and play2Num:
    print("The Dice rolled...")
    print(diceNumber)
    print("both wrong, rolling again...")
    diceNumber = random.randint(1,6)
    time.sleep(1)

if play1Num == diceNumber:
    print("The Dice rolled...")
    print(diceNumber)
    print(play1.upper() + " WINS!")
    print(play2 + " must: " + prize)
    break

if play2Num == diceNumber:
    print("The Dice rolled...")
    print(diceNumber)
    print(play2.upper() + " WINS!")
    print(play1 + " must: " + prize)
    break
\$\endgroup\$
1
\$\begingroup\$

I can't yet comment, so a couple little things.
1. Your print statements are way too long. In your if statement those 4 prints can be one. And why the breaks in your intro statement (let + ' + s)?
2. Your logic gets a little messy in the case of a tie, though I suppose it will print both statements.
3. You may already know this, but you cannot concatenate strings with objects or integers in Python, and due to dynamic typing, it can be hard to keep track of thing.

This whole program could be produced in 2 functions. A function called win, which takes a variable winningPlayer, and a function called Roll for the dice. That would take care of your infinite loop as well.
NOTE: I'm bad with dynamic types. There are probably unnecessary casts to strings here, but I'm just showing the concept.

import random
import time
import sys
#Rolls the dice when players are wrong
def Roll(rollNum):
    print("The Dice rolled..." + str(rollNum))
    print("both wrong, rolling again...")
    reRoll = random.randint(1,6)
    time.sleep(1)
    return reRoll
#Generic function for what the winner and loser do; not player specific
def Win(winner, loser, prz):
    print("The Dice rolled..." + str(diceNumber) + "\n" + str(winner).upper() + "Wins\n" + str(loser) + " must " + str(prz))
#Copied from your code
print("lets play dice of destiny")
play1 = input("Player 1 name?")
play2 = input("Player 2 name?")
prize = input("What does the loser have to do?")
#slightly condensed your code
print("Hi " + play1 + " & " + play2 + ", let's roll the dice of destiny.")
#moved this to a more appropriate place
diceNumber = random.randint(1,6)
play1Num = int(input(play1 + " choose a number 1-6."))
play2Num = int(input(play2 + " choose a number 1-6."))
#changed while loop to roll the dice every time both are wrong
while play1Num != diceNumber and play2Num != diceNumber:
    diceNumber = Roll(diceNumber)
    play1Num = int(input(play1 + " choose a number 1-6."))
    play2Num = int(input(play2 + " choose a number 1-6."))
#Applies the win method to both players; an array would be a better approach
if play1Num == diceNumber:
    Win(play1, play2, prize)
elif play2Num == diceNumber:
    Win(play2, play1, prize)
#Array implementation of win (with more than 2 players, would be best done using *args in win)
players = [play1Num, play2Num]
for item in players
    if diceNumber == item:
        players.remove(item)
        Win(item, players[1], prize)
\$\endgroup\$
  • \$\begingroup\$ Thanks @nathanielsokolow this teaches me a lot. Only issue I have with yours is having to re-pick numbers every time both are wrong, I wanted the original numbers to stay in play. Thanks again \$\endgroup\$ – Mike Wynn Nov 12 '17 at 15:33
  • \$\begingroup\$ Delete the 2 lines after Roll() in the while loop. Did this answer answer your question? \$\endgroup\$ – nathaniel sokolow Nov 12 '17 at 21:32

protected by Community Oct 1 '18 at 13:49

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.