简体   繁体   中英

Getting EOF error but running my code in Thonny produces no errors

I'm learning python and one of my labs required me to:

Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase. The output should include the input character and use the plural form, n's, if the number of times the characters appears is not exactly 1.

My code ended up being:

char = input()
string = input()

count = 0

for i in string:
    if i == char:
        count +=1
        
if count > 1 or count == 0:
    print(f"{count} {char}'s")
else:
    print(f'{count} {char}')

Whenever I run the code in Thonny or in the Zybooks development tab it works but when I select the submit option I keep getting and EOF error:

Traceback (most recent call last):
  File "main.py", line 2, in <module>
    string = input()
EOFError: EOF when reading a line

Does anyone know what's causing the error?

I tried using the break command but it didn't help though I think if I used break at the end of my for statement it wouldn't count all the way. Any ideas folks?

Thank you Mr. Roberts the number of inputs was the issue. I had to create a single input and pull what I needed from that single line. My code ended up being:

string = input()

char = string[0]

phrase = string[1:]

count = 0

for i in phrase:
    
if i == char:
        
count +=1

All good now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM