Cho han game of chance statement

Hi guys, I am attempting to make an if statement that if returns true it informs the user that they have entered an option incorrectly and will prevent the function from running any further.

The code is seen below:

def cho_han(guess, bet):
  global money
  dice_roll_1 = random.randint(1, 6)
  dice_roll_2 = random.randint(1, 6)
  dice_result = dice_roll_1 + dice_roll_2
  print("You have guessed " + str(guess) + " best of luck!")

  if guess != "even" and "odd":
      return "Whoops! you have entered your choice incorrectly! try entering either odd or even to continue"

  elif dice_result % 2 == 0 and guess ==     "even":
    money = money + bet
    return "The combined dice rolls were even, congratulations! You have won £" + str(bet) + "!\n" + "You now have £" + str(money) + " in your account."

  elif dice_result % 2 <= 0 and guess == "odd":
    money = money + bet
    return "The combined dice rolls were odd, congratulations! You have won £" + str(bet) + "!\n" + "You now have £" + str(money) + " in your account."
  
  else:
    money = money - bet
    return "Unfortunately you have guessed incorrectly, better luck next time.\n" + "You now have £" + str(money) + " in your account."

With the call to the function looking something like this:

print(cho_han("odd", 30))

However everytime I call the function it always returns the first if statement.

You have guessed odd best of luck!
Whoops! you have entered your choice incorrectly! try entering either odd or even to continue

Not sure how to get round this issue, when I used it in my first function it worked perfectly fine.

Any help would be appreciated.

Thankyou.

if guess != "even" and "odd":

Your when you have an and or or comparison you need to make sure both sides of the clause are complete.

So it should look closer to:

if guess != "even" and  guess != "odd":

Why can’t the compiler assume you mean guess for both? Because sometimes you want to make statements like:

if guess != "even" and  isValid():

where isValid() is a function that returns a boolean value.

*note that if you just put a single string, it will will pass as True.
Example:

if "pigs fly" or "bread is amazing":
...     print("True")

#output: "True"
#but how would the computer know bread is amazing??
1 Like

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