简体   繁体   English

收到 EOF 错误但在 Thonny 中运行我的代码不会产生任何错误

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

I'm learning python and one of my labs required me to:我正在学习 python,我的一个实验室要求我:

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.编写一个程序,其输入是一个包含一个字符和一个短语的字符串,其 output 表示该字符在该短语中出现的次数。 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. output 应包括输入字符,如果字符出现的次数不正好为 1,则使用复数形式 n's。

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:每当我在 Thonny 或 Zybooks 开发选项卡中运行代码时,它都有效,但是当我 select 提交选项时,我不断收到 EOF 错误:

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.我尝试使用 break 命令,但它没有帮助,尽管我认为如果我在我的 for 语句末尾使用 break 它不会一直计数。 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.现在一切都好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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