Reputation: 11
Working on an assignment in ZyBook and keep getting the following error:
Traceback (most recent call last): File "/home/runner/local/submission/HigherLowerGame.py", line 13, in upper = int(input("What is the upper bound? ")) EOFError: EOF when reading a line
Including my code but I can not figure out what I am doing wrong, can someone please help me?!
**Here are the instructions for the assignment: **
As a part of this lab, you will need to generate a random number. To do this, you have been given starter code that “seeds” the random number generator. Seeding the generator will help you to know the order in which random numbers will be generated. At the top of the program, you have been given the following lines, which “seed” the random number generator.
Do not change these lines:
import random
seedVal = int(input(“What seed should be used? ”)
random.seed(seedVal)
Begin developing your code using the develop mode in zyLabs. For the first iteration, write a program that does the following:
Prompts the user to input the lower bound and upper bound. You must use the variables lower for the lower bound, and upper for the upper bound. Include error checks to ensure that the lower bound is less than the upper bound. You must include these exact prompts: To ask for the lower bound, prompt “What is the lower bound?” To ask for the upper bound, prompt “What is the upper bound?” Includes the following line of code to generate a random number between the lower and upper bounds: random.randint(lower, upper) Prints the generated number. Use this statement to test your program with different input values by checking to make sure that the generated number is between the upper and lower bounds. Note that this will not be in the final version of the game, but for testing we need to know the number that was generated. For the second iteration, remove the print statement that displays the generated number, then modify the code so that it does the following:
Prompts the user to guess a number using the exact phrase “What is your guess?”. Include error checks to ensure that the user only enters values between the lower and upper bound. Prints an output statement based on the guessed number. You must include these exact prompts: If the number guessed is lower than the random number, print out, “Nope, too low.” If the number guessed is higher than the random number, print, “Nope, too high.” If the number guessed is the same as the random number, print, “You got it!” For the third and final iteration, modify the code to loop until the user guesses the number.
Using develop mode, run your code with different sample inputs.
Switch to Submit Mode in the zyLab developer, and click Submit for Grading. The autograder will run your code through different test cases. If you did not pass all the test cases, check the feedback for errors, modify your code, then resubmit. Remember, this is an optional lab and is not required. "Submit for Grading" will run your code against test cases to give you an idea of whether or not it is working.
**IMPORTANT: You must include all possible input values, in order, when using Develop mode, because you will not be able to input additional numbers when the program is running. For example, let's say you wanted to test the game with the following values:
Seed Value: 3 Lower Bound: 1 Upper Bound: 5
You would need to include the following input in the box:
3 1 5 2 5 4 3 1 This would set the seed value as 3, the lower and upper bounds as 1 and 5. Then the test would guess the following numbers, in order: 2, 5, 4, 3, 1. When the correct number is guessed, the program should end. If you do not include all possible values, you may receive errors.
Here is my code so far
import random
seedVal = int(input("What seed should be used? "))
random.seed(seedVal)
# Prompt for lower bound
lower = int(input("What is the lower bound? "))
# Prompt for upper bound with error checking
upper = int(input("What is the upper bound? "))
while upper <= lower:
print("Upper bound must be greater than lower bound.")
upper = int(input("What is the upper bound? "))
# Generate random number between lower and upper bounds
target = random.randint(lower, upper)
# Loop until the user guesses the number
while True:
guess = int(input("What is your guess? "))
# Check if guess is lower, higher, or equal to target
if guess < target:
print("Nope, too low.")
elif guess > target:
print("Nope, too high.")
else:
print("You got it!")
break
Upvotes: 0
Views: 347
Reputation: 11
If you are using VSCode you don't actually need a seed it automatically does it for you and you don't need to type:
seedVal = int(input("What seed should be used? "))
random.seed(seedVal)
But if not just set a seed beforehand I recommend using the seed: 153415562097 as it is long and random. I did this same progject and my code was:
import random
#just to have a bit of fun
easterEggCount = 100
#this is the max guess count
maxGuessCount = 20
# this is the min and max guess
minGuess = 1
maxGuess = 100
playCount = 0
playAgain = True
while playAgain:
playCount += 1
if playCount == easterEggCount:
print("WHY DID YOU WASTE YOUR TIME ON THIS DUMB GAME! DO SOMETHING
BETTER WITH YOUR TIME!")
exit()
num = random.randint (minGuess,maxGuess)
print(f'Welcome to GUESS THE NUMBER! you have {maxGuessCount} attempts
before you lose the game. good luck.')
guess = int(input(f'Guess a number {minGuess}-{maxGuess}: '))
for x in range(maxGuessCount):
if guess < num :
guess = int(input ("the number is larger: "))
if guess > num :
guess = int(input ("the number is smaller: "))
if guess == num :
print ("you got it")
playAgain = input ("play again? (y/n): ")
if playAgain != "y":
playAgain = False
break
Upvotes: 1
Reputation: 11
Your code works fine for me, running in VSCode. How are you executing the file?
According to some other similar questions, it seems this can happen when the script is run in a context where it doesn't have a way to read user input. Try running it in a different editor or through the terminal itself so you can input.
Edit: I'm not familiar with ZyBook, but it seems to be a fairly common issue. There wasn't much of a fix found apart from 'add a new line for multiple vars', so perhaps the ZyBook terminal doesn't automatically enter newlines.
Upvotes: 0