python - 巴马纳沙占卜Python程序
问题描述
我有一项任务需要编写一个 Python 程序来复制 Bamana 的风水实践。
- 实现这一点的第一个任务是编写一个程序,将输入作为单行文本。
- 该行可以包含任意数量的空格或破折号 (-),但不能包含其他字符。
- 给定这条线,显示是否有奇数或偶数个破折号。
- 如果该行中有偶数个破折号,则输出 0。
- 否则,输出 1。
这是我到目前为止写的代码:
dashes = str(input())
mod = dashes % 2
if mod > 0:
print("0")
else:
print("1")
它不起作用,所以有人可以帮助我吗?
谢谢你。
解决方案
你错过了重要的一点:破折号的数量,使用str.count
line = input("Enter a list of dashes: ")
nb_dashes = line.count("-")
mod = nb_dashes % 2
if mod > 0:
print("0")
else:
print("1")
如您所知,您可以使用bool => int
转换来打印 0 或 1
mod = nb_dashes % 2
print(int(mod == 0)) # True > 1, False > 0
推荐阅读
- java-8 - File.exists() 在使用 Java 8.191 的 Windows 10 上有时会出错
- azure-devops - Azure DevOps Server - 在全新的 DevOps Server RC2 上输入正确的许可证?
- xbase - 如何向现有 xBase 文件/表添加新字段/列
- dart - Dart 方法$Class 语法
- excel - 条件格式化锚定到标准单元格的区域
- java - 如何在 Android 中以编程方式更新“.properties”文件中的任何值?
- c# - 在 .NET Framework 中禁用延迟加载 EF6 DBFirst
- java - java.lang.NoClassDefFoundError:无法链接 org/pentaho/di/core/attributes/metastore/EmbeddedMetaStore
- python - Keras 的 expand_dims 函数导致张量丢失元数据
- javascript - 在 2 个独立项目 React Js 和 React Native 之间共享代码的最佳解决方案?(比如 redux 商店)