Please help me figure out what’s wrong with my code. I always get the else statement printed every time! (the Quiz/Exercise)
Feel free to post your way (and probably better!) of writing the code but since I’m trying to learn, I really want to understand why and where my code is wrong as well? So I’m really looking for an explanation of why it’s always printing the same answer?
money = 100
die_1 = random.randint(1, 6)
die_2 = random.randint(1, 6)
bet = (input("Please place your bet"))
odd_or_even = (input("Odd or Even?"))
if die_1 + die_2 == 3 or 5 or 7 or 9 or 11 and odd_or_even == "Odd":
return "Congrats! " + str(die_1 + die_2) + " is Odd so you win " + str(bet) + "€!"
return "Sorry! " + str(die_1 + die_2) + " is Even so you loose " + str(bet) + "€!"
(I didn’t get the joke btw…clearly not a programmer yet )
How I think the if condition works? Well, if something is True then execute the next line of code, if not, then move on…explained in my layman’s terms.
So I read between the lines and I think I got the implicit hint so I put some () here and there to group some operations and now I’m getting the opposite result, i.e. only the if statement is printed!
I can appreciate how you are trying to teach me by not giving me the answer but the risk is just frustration and simply giving up and moving on so could I kindly ask you to just tell me what I did wrong? Don’t worry, I’ll learn from it as well
Oh, sorry. I thought I had done that but I now see that in my OP I hyperlinked to the code and not the Quiz/Exercise, which I hope I have done correctly now!
Just wanted to get that out quickly, I’ll now review the rest of your message and the other poster as well…
No, those operators are very simple, that’s the point.
Or one point, anyway.
Another point is that you should know what they do. You should know what all things you use do. You should be able to argue for everything in your code, or if you can’t then it shouldn’t be there.
The behaviour you were hoping to get from them would require them to do a whole lot of things under the hood, something that they haven’t promised to do. So you cannot expect for it to do what you want.
OR looks at the thing on the left. If it’s truthy, then the result is the value on the left. Otherwise, the result is the value on the right.
^ that is what you need to be leveraging when you use or.
similarly, AND looks at the value on the left. If it’s truthy, then the result is the value on the right. Otherwise, the result is the value on the left.
Geez…thx again ionatan but I’m afraid you’re talking a bit above my head. It’s a bit too cryptic. I can see how you are trying to get me thinking about it to solve it myself but I’m just getting more an more muddled in confusion.
I would really appreciate something closer to the actual solution.
see my description of OR. compare to how you’re using OR
or is an operator with some particular behaviour. to use it you’d need to know what it does. right?
what does or promise to do? that’s the only thing you can use it for, isn’t it?
As for coming up with a solution, that probably involves deciding to do something like … given some number, let’s call it x, compare x to 3, compare x to 5, compare x to 7, compare x to 9, compare x to 11
given those results, combine them in a way that if any of them compared as equal, you’d know. those results are booleans, and you want to combine them into a single boolean, do you know an operator with such behaviour?
and, how would you compare, how can you test whether two things are equal?
there’s an operator for that as well, ==
so you’d need to do all of these:
x == 3
x == 5
x == 7
x == 9
x == 11
And then you would have several booleans.
Or, if you’re looking specifically for odd numbers, then what does odd mean? odd is something that can be tested because odd numbers behave a certain way when divided by 2
Sigh…I need help.
I was about to roughly duplicate my if/return and else/return statements and just change the values to reflect a bet on Odd but I didn’t get very far as PyCharm was not happy with me and giving me an unreachable code error before I could even finish typing!
What am I doing wrong…again!
Hopefully a nested if function isn’t going to be necessary here because I’m not ready for that (that would really do my head in).
Throw me a bone please, I’m at breaking point.
To be fair to myself, maybe I’m going beyond the scope of the exercise? If you read what it says:
Create a function that simulates playing the game Cho-Han. The function should simulate rolling two dice and adding the results together. The player predicts whether the sum of those dice is odd or even and wins if their prediction is correct.
The function should have a parameter that allows for the player to guess whether the sum of the two dice is "Odd" or "Even" . The function should also have a parameter that allows the player to bet an amount of money on the game.
I wanted to have some ‘fun’ by introducing the input function and return print statements for 4 scenarios (win & loose for both Odd & Even) but maybe with the tools that I’ve been taught up to now in the Python 3 course, I’m not expected to be able to do it as I am without some additional knowledge?
What do you think?
Sometimes to move forward, we have to take a step back. Consider what your function must do. What are the steps? My take on the steps:
generate random int between 2 and 12 inclusive
get user input:
guess (even or odd)
determine if random int is even or odd
determine if user’s guess matches random int (even or odd)
print you won or you lost depending on previous step (#4)
You’ve come close to implementing these steps. Consider what you actually need to know to determine whether the user has won or lost. Instead of comparing many possibilities using several if statements, you really only need one. You just need to know if the random int is even or odd, and if the user guessed correctly. What if you determine whether the random int is even or odd, and assign a value reflecting that to a variable:
if random_int % 2 == 0:
even_odd = "Even"
even_odd = "Odd"
#could use a ternary expression instead if you are familiar with those:
even_odd = "Odd" if random_int % 2 else "Even"
#random_int % 2 returns 1 or 0. If it returns 1 that is truthy, so "Odd" is assigned, otherwise "Even" is assigned
Then the user’s guess can be compared to that value. If it matches, they won. If not, they lost.
I gave it a shot with your suggestions but…same thing!
dice_roll = random.randint(2,12)
bet = (input("Please place your bet"))
odd_or_even = (input("Even or Odd?"))
if dice_roll % 2 == 0:
odd_or_even = "Even"
odd_or_even = "Odd"
if bet == odd_or_even:
return "Dice roll was " +str(dice_roll) + " You win!"
return "Dice roll was " +str(dice_roll) + " You lose"