python - Python:IDE中的代码是正确的,但在作业中是不正确的
问题描述
所以我正在做 Python 作业,我更喜欢在 IDE 中写作业的答案,然后复制并粘贴我的答案。对于这个特定的问题,我就是这样做的,虽然代码在 IDE 中运行良好,但在作业中被标记为不正确。
作业问题:编写一个从标准输入读取字符串的循环,其中字符串是“duck”或“goose”。读入“goose”时循环终止。循环之后,您的代码应打印出已读取的“duck”字符串的数量。
我的作业说: 检测到的问题:⇒ _stdout 的值不正确。
我的答案:
duckcount = 0
animal = ''
while True:
animal = input('enter animal')
if animal == 'duck':
duckcount +=1
elif animal == 'goose':
break
print(duckcount)
该代码在我的 IDE 中运行良好,但我在作业中收到的错误消息是:_stdout 的值不正确。
解决方案
我想知道您的老师是否由于您的输入字符串默认没有任何空格而在输入中添加了自己的空格。
尝试这个:
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!
推荐阅读
- r - 是否有一个 R 函数可以根据另一列中的最小值删除索引变量的重复项?
- amazon-web-services - 如何将查询字符串和标头映射到 AWS C# lambda 函数参数
- spring - 用dokku部署spring api:app容器启动失败
- javascript - Ajax.Reload - 不刷新数据表
- android - java.lang.IllegalArgumentException onActivityResult
- javascript - Vue.js CLI 3 - 如何为 CSS/Sass 创建供应商包?
- keras-layer - 关于 Keras 嵌入层的一个快速问题
- unity3d - Photon Engine:客户端无法连接到现有房间并创建同一个房间
- unity3d - 使用虚拟按钮控制多个图像对象的行为
- xml - Powershell使用-replace编辑节点中的部分文本?