-1

I'm a beginner and I can't figure out why I can't get the output I want. It's a craps game. It's suppose to go like:

How many games do you want to play > 6 You rolled 5 + 2 = 7 You win

What I got is something like: You rolled 1 + 6 = 7 You rolled 1 + 6 = 7 You rolled 1 + 6 = 7 You lose

import random


def rollDice():
  roll_1 = random.randint(1,7)
  roll_2 = random.randint(1,7)
  return roll_1, roll_2

def determine_win_or_lose(dice1,dice2):
  sum = dice1 + dice2
  print("You rolled", dice1, "+", dice2, "=", sum )
  if sum == '2' or '3' or '12':
    return 0
  elif sum == '7' or '11':
    return 1
  else:
    print("Point is", sum)
    determinePointValueResult(sum)
    if determinePointValueResult(sum) == 1:
      return 1
    elif determinePointValueResult(sum) == 0:
      return 0
  

def determinePointValueResult(sum):
  point = sum
  while sum != 7 and sum != point:
    x, y = rollDice()
    sum = x + y
    if sum == point:
      return 1
    elif sum == '7':
      return 0
    print("You rolled", x, "+", y, "=", sum )
 

#==== MAIN =====
win = 0
lose = 0

game = int(input("How many games do you want to play > "))

for i in range(game): 
  x, y = rollDice()
  determine_win_or_lose(x, y)
  if determine_win_or_lose(x, y) == 1:
    print("You win")
    win = win + 1
  elif determine_win_or_lose(x, y) == 0:
    print("You lose")
    lose = lose + 1

print("Game results: ", win, "wins and", lose, "losses")
  • 1
    This kind of code `if sum == '2' or '3' or '12':` does not work like you think it should. See [Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?](https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true-how-can-i-compare-a-to-al) – Gino Mempin Aug 11 '22 at 15:29
  • 1
    You call `determine_win_or_lose()` two or three times per game; each call will result in the "You rolled..." message being printed. Call it *once*, and save the result in a variable so you don't need to call it again. – jasonharper Aug 11 '22 at 15:30

2 Answers2

0

Obvious issues:

  1. You call determine_win_or_lose too many times in your for loop. Change it to:
for i in range(game): 
  x, y = rollDice()
  result = determine_win_or_lose(x, y)
  if result == 1:
    print("You win")
    win = win + 1
  elif result == 0:
    print("You lose")
    lose = lose + 1
  1. Your check in determine_win_or_lose is incorrect. It should be something like:
def determine_win_or_lose(dice1,dice2):
  sum = dice1 + dice2
  print("You rolled", dice1, "+", dice2, "=", sum )
  if sum == 2 or sum == 3 or sum == 12:
    return 0
  elif sum == 7 or sum == 11:
    return 1
  else:
    print("Point is", sum)
    determinePointValueResult(sum)
    if determinePointValueResult(sum) == 1:
      return 1
    elif determinePointValueResult(sum) == 0:
      return 0
  1. In determinePointValueResult you shouldn't compare sum to a string, but an integer:
def determinePointValueResult(sum):
  point = sum
  while sum != 7 and sum != point:
    x, y = rollDice()
    sum = x + y
    if sum == point:
      return 1
    elif sum == 7:
      return 0
    print("You rolled", x, "+", y, "=", sum )
  1. It's possible that determine_win_or_lose and determinePointValueResult are returning None. You may need to change your elifs to elses or create a new else case.
Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
0

Your issue come from the main, because you call the determine_win_or_lose function 3 times, the first one before the if (and i'm not sure why since you do nothing with it), a second time to check the condition of the if and a third time to check the condition of the elif.

Since it's this function that print the message, and you call the function 33 times each iteration of the for loop, it's normal to get the message printed 3 times.

( Also since the determine_win_or_lose will always return 0 or 1 you don't really need an elif you can just do an if/else to achieve the same thing and simplify your code a bit. )

So i'd suggest the following :

#==== MAIN =====
win = 0
lose = 0

game = int(input("How many games do you want to play > "))

for i in range(game): 
  x, y = rollDice()
  result = determine_win_or_lose(x, y)
  if result == 1:
    print("You win")
    win = win + 1
  else:
    print("You lose")
    lose = lose + 1

print("Game results: ", win, "wins and", lose, "losses")
Xiidref
  • 1,456
  • 8
  • 20