My input code is not printing to the terminal

I’m doing the games of chance project and I can get my input statement to print. I keep getting an EOF on the flip a coin project

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

Thanks for your help

Hey there.

Can you post the code please? :slight_smile:

def flip_coin( bet, money):
    num = input("Heads or Tails? Press 1 or 2.")
    ans = random.randint(1,2)
    if ans == 1:
      print("The coin is heads")
    if ans ==2 :
      print("The coin is tails")
      if num == ans:
        money += bet
        print("You win" + str(bet))
      else:
        money -= bet
        print("You lose")

That is not the entirety of your program… is it?

(I suspect not, since you’re calling random.randint() with no import in that snippet…)

It isnt because I got this

Heads or Tails? Press 1 or 2.Traceback (most recent call last):
File “script.py”, line 58, in
flip_coin(55, money)
File “script.py”, line 7, in flip_coin
num = input(“Heads or Tails? Press 1 or 2.”)
EOFError: EOF when reading a line

u know what screw it

import random
def games(money = 100):

#Write your game of chance functions here
  def flip_coin( bet, money):
    num = input("Heads or Tails? Press 1 or 2.")
    ans = random.randint(1,2)
    if ans == 1:
      print("The coin is heads")
    if ans ==2 :
      print("The coin is tails")
      if num == ans:
        money += bet
        print("You win" + str(bet))
      else:
        money -= bet
        print("You lose")
  def cho_han(guess, bet, money):
    die_1 = random.randint(1,6)
    die_2 = random.randint(1,6)
    print('You rolled a ' + str(die_1) + ' and ' + str(die_2))
    if (die_1 + die_2) % 2 == 0:
      ans = 'Even'
      print('The roll is Even')
    else:
      ans = 'Odd'
      print('The roll is Odd')
    if guess == ans:
      money += bet
      print('You win ' + str(bet))
      print('Your total is ' + str(money))
      return money
    else:
      money -= bet
      print('You lose ' + str(bet))
      print('Your total is ' + str(money))
      return money
  def pick_a_card(bet, money):
    deck = []
    test_deck = [[c,c,c,c] for c in range(1,14)]
    for d in test_deck:
      for c in d:
        deck.append(c)
    card_1 = random.choice(deck)
    deck.remove(card_1)
    card_2 = random.choice(deck)
    deck.remove(card_2)
    if card_1 > card_2:
      money += bet
      print('Player 1 wins!')
      print('Your total is ' + str(money))
      return money
    if card_2 > card_1:
      money -= bet
      print('Player 2 wins!')
      print('Your total is ' + str(money))
    else:
      print('Its a tie!')
      print('You win nothing')

#Call your game of chance functions here
flip_coin(55, money)
#cho_han('Even', 10, money)
#pick_a_card(33, money)

This is all of of it

The link is more helpful imo

The link is to the project information, and provides us with no access to your code. Your code is what is producing errors, and is what you’re here asking for help with. The link provides the context for your program, nothing more.

…and I am reasonably confident it is not what you were running when you got this error:

Heads or Tails? Press 1 or 2.Traceback (most recent call last):
File “script.py”, line 58, in
flip_coin(55, money)
File “script.py”, line 7, in flip_coin
num = input(“Heads or Tails? Press 1 or 2.”)
EOFError: EOF when reading a line

because when I run what you posted, I get this:

Traceback (most recent call last):
  File "eoferr.py", line 62, in <module>
    flip_coin(55, money)
NameError: name 'flip_coin' is not defined

The error I am seeing is because you’ve got a random function declaration on line 2, def games(money = 100): which is immediately followed by more function declarations. Subsequently, when we try and call flip_coin Python flips out (pun intended) because there’s no function of that name within the global scope… The function flip_coin exists, for some reason, within the definition of another function…

I’m not sure whether you intended games() to be a class - a Python analogy of a casino or arcade, perhaps - where many games reside with a shared wallet (the money variable)?

Are you annoyed that I asked you for the entirety of your program?

Edit: If I fix your code, by which I mean get rid of the def games() bit and step the indenting back for everything else, your program runs. I get:

Heads or Tails? Press 1 or 2.2
The coin is tails
You lose

I have a sneaking feeling, then, that the EOF you’re getting is unrelated to the program and rather it’s because of some weird behaviour we occasionally see with the Codecademy editor… It does, sometimes, take issue with the input() function in Python and throws EOF errors where they don’t occur in another environment.

If it is that you’re working in the Codecademy editor, just to double-check if it is this you can run the Python code locally on your machine to see if the error happens there also…

1 Like

Let me try this again. I will post the original code with the error

I’ve edited my previous post with more info…

`import random

money = 100

#Write your game of chance functions here
def flip_coin( bet, money):
  num = input('Heads or Tails? Press 1 or 2:')
  ans = random.randint(1,2)
  if ans == 1:
    print("The coin is heads")
  if ans ==2 :
    print("The coin is tails")
    
def cho_han(guess, bet, money):
  die_1 = random.randint(1,6)
  die_2 = random.randint(1,6)
  print('You rolled a ' + str(die_1) + ' and ' + str(die_2))
  if (die_1 + die_2) % 2 == 0:
    ans = 'Even'
    print('The roll is Even')
  else:
    ans = 'Odd'
    print('The roll is Odd')
  if guess == ans:
    money += bet
    print('You win ' + str(bet))
    print('Your total is ' + str(money))
    return money
  else:
    money -= bet
    print('You lose ' + str(bet))
    print('Your total is ' + str(money))
    return money
def pick_a_card(bet, money):
  deck = []
  test_deck = [[c,c,c,c] for c in range(1,14)]
  for d in test_deck:
    for c in d:
      deck.append(c)
  card_1 = random.choice(deck)
  deck.remove(card_1)
  card_2 = random.choice(deck)
  deck.remove(card_2)
  if card_1 > card_2:
    money += bet
    print('Player 1 wins!')
    print('Your total is ' + str(money))
    return money
  if card_2 > card_1:
    money -= bet
    print('Player 2 wins!')
    print('Your total is ' + str(money))
  else:
    print('Its a tie!')
    print('You win nothing')

#Call your game of chance functions here
flip_coin(55, money)
#cho_han('Even', 10, money)
#pick_a_card(33, money)`
call last):
  File "script.py", line 58, in <module>
    flip_coin(55, money)
  File "script.py", line 7, in flip_coin
    num = input('Heads or Tails? Press 1 or 2:')
EOFError: EOF when reading a line

That’s what I changed your previously posted code to…

Am I right that you’re writing this program online in the Codecademy environment?

Yeah. So how do I complete the project?

I assume that the Python projects work the same as the ones for Go(lang)… where the Codecademy environment doesn’t actually assess your work, and instead you just check off the tasks as you complete them. (This would fit with the possibility of completing them in your own local environment, rather than on Codecademy.)

I’d try the usual troubleshooting stuff - clear cache, restart browser etc etc - but if you’re still getting the EOF from the CC environment you may need to finish the project with a local install of Python…

If you dont mind me asking, what coding anything are you working on right now?

a fair bit of exercises only display output, there’s no input, which is why you hit end of file when you try to read input

1 Like

I’m tinkering about with Go. Not building anything in particular, just trying to get the basics. :slight_smile: