# Game of Chance project

Hello,

I am new to coding and I am working a Python Project called Game of Chances. I am having a hard time getting my random number variable called sum_total to check for odd ad even numbers and give the proper result in my code. Currently if I choose an ‘odd’ argument the function always chooses the first if statement on line 50. If I choose the ‘even’ argument I get the else statement on line 52. Any help would be greatly appreciated. Thank you so much for taking the time to help me.

Game of Chance Project

``````def cho_han(guess, bet):
num1 = random.randint(1,6)
num2 = random.randint(1,6)
sum_total = num1 + num2
result = ''
if sum_total == 1 or 3 or 5 or 7 or 9 or 11:
result = 'odd'
else:
#sum_total == 2 or 4 or 6 or 8 or 10 or 12:
result = 'even'

if guess == result:
return "Your dice roll was the number " + str(sum_total) + ". \n\nYour guess was an " + guess + " number.\n\nYou win " + str(bet) + " coins.\n\nYou now have " + str(money + bet) + " coins!"
else:
return "Your dice roll was the number "  + str(sum_total) + "! \n\nYour guess was an " + guess + " number.\n\nYou lose " + str(-bet) + " coins.\n \nYou now have " + str(money - bet) + " coins!"

print(cho_han('odd', 10))
``````

Hello, @godsmafia. Welcome to the forum!
If you would paste your code into a post we could be of more help. Please click on the `</>` button in the menu bar, and then paste your code from the exercise into the space indicated. It will make it far easier for the rest of us to work with your code, and help you find the problem. Thanks!

1 Like

Hello midlindner,

Thank you for that tip. I did what you had mentioned. I look forward to your help and response.

This line won’t work like you’re expecting. You’ll need to apply one of these fixes:

``````if sum_total == 1 or sum_total == 3 or sum_total == 5 or sum_total == 7 or sum_total == 9 or sum_total == 11:
``````

If you’re familiar with the modulo operator, this will do the same, and is much shorter:

``````if sum_total % 2 != 0:
``````

Happy coding!

1 Like

Hello Midlindner,

Thank you so much for that. I was soooooo close. I appreciate you helping me out. Question…Can you please explain the shorter version of the modulo operator? I don’t seem to be grasping this syntax that well in relation to my code. Thanks again for everything.

2 Likes

The modulo operator `%` performs a division operation, and returns the remainder. For example: 6 / 4 = 1 with a remainder of 2, so 6 % 4 = 2. When we check for even or odd, we can see if the remainder of x % 2 == 0.

``````if x % 2 == 0: #even number
if x % 2 != 0: #odd number (!= means 'not equal' forgive me if the clarification was unnecessary)
``````
1 Like

Actually…I was thinking that != 0 meant meant is not equal to zero. Wow I am really off. What does the != 0 mean in the is line of code. Thank you so much for all your help

Operators, in general, do something with the value on the left, and on the right. What values are on the left and right of `!=`? Same thing with `or` and `==` and all the other binary (those that take two arguments) operators.

There also exists a module with function versions of operators.

``````from operator import *

ne(mod(x, 2), 0)
``````

Those are usually not convenient for direct use (just use the operator instead) but perhaps that helps you see them better for what they are.

Operators behave the same way as the operators that are taught in elementary school. Evaluate them in order to get the result. Some operators have higher precedence than others - multiplication is carried out before addition.

1 Like

Thank you so much for clarifying these thing. What is this code mean:

``````if x % 2 != 0:
``````

If you know what the individual operators there do then you know as much as me. There’s no further meaning.
Maybe if you attempt to read it out loud to make yourself actually read it?
If there’s any part of it that you don’t know what it does, then stop trying to understand the whole thing, and figure out what that part does first.

Ok. Thank you for your help.

You could also look at it from the perspective of what the overall problem being solved is. That should tell you a lot about what actions need to happen, and then you can start matching them to the code.
Or, if you only know the problem and you need to solve it using code, then it’s a matter of considering how you can leverage various operations to get to where you want. Think of it like lego blocks.
You can also write out results to your terminal, to see what intermediary values are if you’re not already sure.

Those are some good suggestions. I really appreciate you laying that out for me.

Hello to everyone. I’m also new here. Seeing all so involved in coding, I’m starting to think it would be a good idea if kids could also learn to code from a younger age. I’m talking about complicated things, just the basics. A good idea would be to have games for learning to code, especially for kids. They would joyfully offer information, and kids will not even observe how they learn something useful. This way, they would also develop computational thinking.