Constant Unexpected EOF while parsing error with Games of Chance project

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

Basically what the post title says. I’m tackling the games of chance project. I got through the coin flip function and it worked fine. Then I attempted the Cho Han game. When I would run that function I would always get “Unexpected EOF while parsing” error. So I looked up what that meant and it turned out it could means a number of things, but since syntax is usually what I mess up, I went through my code again. Still couldn’t find the issue. So I tried to run the function with cmd and it worked (although I would only ever get the else statement to print. After trying this a few times, I again started getting Unexpected EOF while parsing in CMD! So now I’m at my wits end. I just can’t figure the issue out. Gonna leave the code here in the hopes someone can find my error and give me some guidance. TIA!

import random

money = 100

#Write your game of chance functions here

def coin_flip(guess, player_bet):
  outcome = random.randint(1, 2)
  if money < player_bet:
    print("You don't have enough money to make that bet!") 
  elif guess == "Heads" and outcome == 1:
    print("Heads! You just won $" + str(player_bet) + "!")
  elif guess == "Tails" and outcome == 2:
    print("Tails! You just won $" + str(player_bet) + "!")
  else:
    return "You lose! You just lost -$" + str(player_bet) + "!"


def cho_han(guess, player_bet):
  dice1 = random.randint(1, 6)
  dice2 = random.randint(1, 6)
  
  if money < player_bet:
    return "You don't have enough money to make that bet!"
  elif guess == "Even" and guess == dice1 + dice2 % 2 == 0:
    return "It's Even! You won $" + str(player_bet) + "!"
  elif guess == "Odd" and guess == dice1 + dice2 % 2 != 0:
    return "It's Odd! You won $" + str(player_bet) + "!"
  else:
    return "Nope! You lost -$" +str(player_bet) + "!"

Hey there @donwick!!

Is this all of your code? It currently work runs without error for me, even if I call your function.

My first guess would be that you tried to use an input().
Not all of CC’s consoles are capable of collecting input, and throw an EOF error if it is attempted.

Yeah I had previously read about CC’s problems with inputs on another post. I did have some inputs originally, but got rid of them after learning about the problem. Still am having the issue on my end. Thanks for taking a look!

1 Like

Is that your full code that you posted?

You could try telling the cc terminal to run python3 explicitly.

How would I go about doing that toasted? Total noob so sorry if it’s something commonly known.

Yes it’s the full code.

I think you just type python3 and enter. I remember doing this when doing the codecademy cs path.

How did you run the function? You never call it in the code provided.

I don’t think you can access the console in this project, so I am not sure you can run Python 3.

Ahh I see, interesting.

Unless I am missing something, it should actually be running Python3 already since you are using the print function:

print("Hello World")

instead of the statement:

print "Hello World"

@donwick

For reference you can always verify with

import sys
print(sys.version)
2 Likes

I was calling the function with a print statement like:

print(cho_han(guess, player_bet))

with values for the arguments obviously.

Unfortunately print appearing as function won’t tell you for certain what the version is since almost all the lessons import it in the background (i.e. you can call it in every lesson I’ve tried with Python2 without writing your own import statement). For reference the version used in 3.6.9 so it’s relatively up to date. I tried using input() and it is not set-up for this lesson (EOF error) so you’ll have to do without that.

I tried copy-pasting your code above into that page and adding a statement to call cho_han and then print the output seemed which seemed to work just fine for me (standard output of you lost $ money) . At least there was no errors picked up when parsing or running (I’ve not checked if the actual code does what it’s supposed to).

Are you running this off-platform? If so what’s the set-up, how are you calling it etc.

Yes I tried running this off-platform using Atom and CMD console. The functions work, except I’m experiencing the same thing with the you lost $ output for the cho_han function. Seems to be the only output I ever get and have not figured that out yet. I updated my code a bit today as well. I’ll leave it behind if anyone wants to check it.
Also it would seem that I’m not getting the EOF error today. I’m beginning to wonder if this is some kind of issue with CC’s console and/or connection. Sometimes I take so long looking over my code that I get disconnected. Hate to be “that guy” who blames his tools though.

import random

money = 100

#Write your game of chance functions here

def coin_flip(guess, player_bet):
  outcome = random.randint(1, 2)
  
  if money < player_bet:
    print("You don't have enough money to make that bet!") 
  elif guess == "Heads" and outcome == 1:
    print("Heads! You just won $" + str(player_bet) + "!")
  elif guess == "Tails" and outcome == 2:
    print("Tails! You just won $" + str(player_bet) + "!")
  else:
    return "You lose! You just lost -$" + str(player_bet) + "!"


def cho_han(guess, player_bet):
  dice1 = random.randint(1, 6)
  dice2 = random.randint(1, 6)
  total = dice1 + dice2
#if player bets more money than they have
  if money < player_bet:
    print ("You don't have enough money to make that bet!")
  elif player_bet <= 0:
    print ("Your bet needs to be more than zero.")
  
  elif guess == "Even" and guess == total % 2 == 0:
    print ("It's Even! You won $" + str(player_bet) + "!")
    return player_bet
  elif guess == "Odd" and guess == total % 2 == 1:
    print ("It's Odd! You won $" + str(player_bet) + "!")
    return player_bet
  else:
    print ("Sorry! You lost -$" +str(player_bet) + "!")
    return -player_bet

All the game functions should be returning a number, only. Handle the ‘money’ on the return. There should be no need to bring up money in any of the functions.

So I cleaned up my code today and removed the money variable from the functions. Coin flip works fine. However, cho_han() only ever prints the else statement, both in CC’s console as well as CMD with Atom. I’ll leave it here if anyone can spot why it’s doing this…

import random

money = 100

#Write your game of chance functions here

def coin_flip(guess, player_bet):
  outcome = random.randint(1, 2)
  
  #if player doesn't bet any money
  if player_bet <= 0:
    return "Your bet needs to be more than zero."
    
  elif guess == "Heads" and outcome == 1:
    print("Heads! You just won $" + str(player_bet) + "!")
    
  elif guess == "Tails" and outcome == 2:
    print("Tails! You just won $" + str(player_bet) + "!")
    
  else:
    return "Sorry! You lost -$" + str(player_bet) + "."
    return -player_bet


def cho_han(guess, player_bet):
  dice1 = random.randint(1, 6)
  dice2 = random.randint(1, 6)
  result = dice1 + dice2

#if player doesn't bet any money
  if player_bet <= 0:
    return "Your bet needs to be more than zero."
  
  elif guess == "Even" and guess == result % 2 == 0:
    return "It's Even! You won $" + str(player_bet) + "!"
    
  elif guess == "Odd" and guess == result % 2 == 1:
    return "It's Odd! You won $" + str(player_bet) + "!"

  else:
    return "Sorry! You lost -$" +str(player_bet) + "!"
    


#Call your game of chance functions here

#Call your game of chance functions here

The thing to do at first is get the functions to do what they’re supposed to and skip the verbosity. Just return the bet on a win, and negative bet on a loss. Print it at the caller, and adjust the money account accordingly.

money = 100
# loop around this
    bet = 10
    a = coin_flip(bet)
    money += a
    print (money)

Finally figured out what was causing the problem with my cho_han function. In my if, elif statements I had
guess == " " and guess % 2== when it should have been guess == " " and result % 2 ==

Man do I feel dumb, relieved and exhausted all at once. Thanks for the input everyone!

1 Like