因此,我正在做Python作业,我更喜欢在IDE中编写作业的答案,然后复制并粘贴我的答案。对于这个特定的问题,我就是这么做的,虽然代码在IDE中运行良好,但它在作业中被标记为不正确。
家庭作业问题:编写一个从标准输入中读取字符串的循环,其中字符串是“鸭子”或“鹅”。当"goose“被读入时,循环终止。在循环之后,您的代码应该打印出所读取的"duck“字符串的数量。
我的作业内容:检测到的问题:⇒_stdout的值不正确。
我的答案是:
duckcount = 0
animal = ''
while True:
animal = input('enter animal')
if animal == 'duck':
duckcount +=1
elif animal == 'goose':
break
print(duckcount)代码在我的集成开发环境中运行良好,但我在家庭作业中得到的错误消息是:_stdout的值不正确。
发布于 2019-10-22 02:46:14
我想知道你的老师是否因为你的输入字符串默认没有空格而在输入中添加了他们自己的空格。
试试这个:
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))结果:
enter animal: duck
enter animal: duck
enter animal: duck
enter animal: goose
There are 3 ducks!https://stackoverflow.com/questions/58492336
复制相似问题