Python: Code in IDE is correct, but is incorrect in homework

So I am working on Python homework, and I prefer to write the answers to the homework in an IDE, then copy and paste my answer. For this specific problem I did just that, and while the code works in the IDE just fine, it is marked as incorrect on the homework.

Homework question: Write a loop that reads strings from standard input, where the string is either "duck" or "goose". The loop terminates when "goose" is read in. After the loop, your code should print out the number of "duck" strings that were read.

What my homework says: Problems Detected: ⇒ The value of _stdout is incorrect.

My answer:

duckcount = 0 animal = '' while True: animal = input('enter animal') if animal == 'duck': duckcount +=1 elif animal == 'goose': break print(duckcount) 

The code works fine in my IDE, but the error message I get on my homework is: The value of _stdout is incorrect.

3

4 Answers

I am wondering if your teacher added their own spaces to the input due to your input string not having any by default.

Try this:

duckcount = 0 animal = '' while True: animal = input('enter animal: ').strip() if animal == 'duck': duckcount += 1 elif animal == 'goose': break if duckcount == 1: print('There is {} duck!'.format(duckcount)) else: print('There are {} ducks!'.format(duckcount)) 

Results:

enter animal: duck enter animal: duck enter animal: duck enter animal: goose There are 3 ducks! 

Thank you for sharing this! I was recently working on this question as well, but with the specific instruction to leave the input function without a prompt string. Your original code ran just fine for me in IDLE, but I had to do the following to get the answer accepted by my Pearson textbook:

duckcount = 0 animal = '' while True: animal = input() if animal == 'duck': duckcount +=1 elif animal == 'goose': break print(duckcount) 

OK I figured it out, user10987432 gave me this idea. All i did was replace 'enter animal' with ''. I guess I was supposed to just use a blank space for an input. (Which works but I like having a little message on what to do as a preference). thanks for all the responses!

0

_stdout is the output that your program is writing to when it prints things on the command line. The logic of your program is perfetly fine as it is.

Since the output is marked as being incorrect I would first ask the teacher if he wants a specific wording. I expect that he/she probably hardcoded a specific output into the Homework-Parser. If that's the case then just change your print(duckcounter) to whatever sentence/output the teacher hardcoded (as I said the best was would be to ask him/her if she/he has done that).

As said by Mike - SMT the teacher could have modified the input statement as well. You can think about the input as a two-part command. First it prints something out to _stdout (output on the command line) and then waits for a specific action (usually that you type something in and hit enter).

You could as well do something like this:

animal = '' counter = 0 while animal != 'goose': animal = input('Please enter either "Goose" or "Duck": ') animal = animal.lower() # make everything lowercase if animal == 'duck': counter += 1 print('You have typed "duck" {} times'.format(counter)) 

If you have any questions let me know.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like