I am completely stuck, would anybody care to help?

https://www.codecademy.com/practice/projects/games-of-chance

I am completely lost, neither of my programs work and I just don’t know what to do. Please help!!!

import random
money = 100
num = random.randint(1, 2)
dice = random.randint(1, 6)
even = 2 or 4 or 6
odd = 1 or 3 or 5
#Write your game of chance functions here
def heads_or_tails(bet, guess):
	if guess == 1 and num == 1:                       	
		return bet + money
	elif guess == 2 and num == 2:
		return bet + money
	elif guess == 1 and num == 2:
		return money - bet * -1
	elif guess == 2 and num == 1:
		return money - (bet * -1)
	else:
		return money - (bet * -1) 
  
def Cho_Han(bet, guess):
	if dice + dice == odd and guess == odd:
		return money + bet
	elif dice + dice == even and guess == even:
		return money + bet
	elif dice + dice == even and guess == odd:
		return money - (bet * -1)
	elif dice + dice ==  odd  and guess == even: 
		return money - (bet * -1)
	else:
		return money - (bet * -1)
#Call your game of chance functions here
print(heads_or_tails(1, 2))
print(Cho_Han(100, odd))


1 Like

Got a question?
Try describing both what is currently happening and what you would like to happen instead. Also, what is preventing you from eliminating that difference? Narrow it down as much as you can, talk about a specific operation rather than the program as a whole.
For testing and narrowing things down you may want to use print statements to write out what is being done to check whether the program has all the information it should have at that time and whether it’s correct. You might for example write out the information before and after you carried out some operation to see whether it had the desired effect.

Something like this will never work:

dice + dice == odd

Because 5 is not equal to the concept of odd.
For example, if 5 was equal to odd, then odd would be 5. 3 would not be equal to odd because odd is 5, remember? What you’d want to do instead is to ask yourself what being odd means - it has to do with the result of dividing by two - so what you would need to do in order to test it, is to divide by two and look at the result.

Something that may help a great deal is to break out operations into their own functions. For example, you could make a function is_odd which would take a single number as input and return a boolean indicating whether the number is odd. This allows you to focus on solving the “is this odd?” problem in isolation from everything else.


	if guess == 1 and num == 1:
	elif guess == 2 and num == 2:
	elif guess == 1 and num == 2:
	elif guess == 2 and num == 1:

this could be generalized to just:

if guess == num

yes? no need to repeat the comparison for each possible value, the only interesting information from that is whether or not they are equal.


When figuring out how the code should carry out an operation, try doing the operation yourself, maybe with pen and paper, and study how you yourself do it. Your code should probably be doing the very same, nothing more or less.
Do not write it down as if it was speech “is x odd? -> x == odd” because speech can sometimes be very sloppy and avoid saying what’s actually meant.

4 Likes

For one thing, this will not work as you think it will:

even = 2 or 4 or 6
odd = 1 or 3 or 5

The or operator is meant to return True or False when used with operands (the things on its right and left) that evaluate to True or False. However, with other objects, such as the ints you are using, it will return the first non-false (which with int means non-zero) operand.

Bottom line, in your configuration above, even will always be 2 and odd will always be 1. Put in a print() statement to see if I’m right.

How to fix? One way would be to say even = [2,4,6] and odd = [1, 3, 5], then use the in operator to check. Easier perhaps to check whethernum % 2 == 0 . If so, num is even, if not, odd.

Now,

	if guess == 1 and num == 1:                       	
		return bet + money
	elif guess == 2 and num == 2:
		return bet + money
	elif guess == 1 and num == 2:
		return money - bet * -1
	elif guess == 2 and num == 1:
		return money - (bet * -1)
	else:
		return money - (bet * -1) 

How about:

if guess == num:
     # do something
else:
    # do something else

For Cho Han, you have dice + dice, which will always be even, since dice is selected via randint() only one time, hence each “roll” of the dice will always show the same nimber on each die. Again, use print() to check this out. Each die should get its own “roll.”

3 Likes

Minus a minus is adding. Is that what you want to do?

return money + bet * -1

will subtract the bet from the money.

You might want to create a couple of helper functions to generate random values and to check parity.

from random import randrange as toss

def get_toss(intervals):
    return toss(0, intervals)

def is_even(n):
    return  n % 2 == 0

def heads_or_tails(bet, guess='heads'):
    flip = get_toss(2)
    if guess == ['heads', 'tails'][flip]:
        return bet
    else:
        return -bet

We’ve left out the money variable so will have to handle that on the call return.

money += heads_or_tails(2, 'tails')
print (money)

If we wish to manipulate the money inside the function, then we need to declare a binding to that global in the function else we cannot write to it.

 global money

Now the value can be modified by the function, although we need to decide if that’s what we want to do. One school of thought is that functions should have a very simple purpose and not make too many things happen.

Given the above example should help you to simplify Cho_Han.

4 Likes

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.