python - Python初学者:if语句不返回打印函数
问题描述
我最近尝试用这段代码做一个爱情计算器。但是,当面对一个包含超过 11 个字符的“love”或“true”共同字符的名称时,它不会返回正确的语句。例如,如果我因为“love”语句超过 9 而返回 711,它只会给我“else”选项而不是 => 90 语句。我不确定我做错了什么。感谢您提前提供任何帮助!
print("Welcome to the Love Calculator!")
name1 = input("What is your name? \n")
name2 = input("What is their name? \n")
combined_names = str(name1.lower()) + str(name2.lower())
t = combined_names.count('t')
r = combined_names.count('r')
u = combined_names.count('u')
e = combined_names.count('e')
l = combined_names.count('l')
o = combined_names.count('o')
v = combined_names.count('v')
e = combined_names.count('e')
Love = l + o + v + e
true = t + r + u + e
truelove = int(str(true) + str(Love))
if truelove <= 10 and truelove >= 90:
print(f"Your score is {truelove}, you go together like coke and mentos")
elif truelove >= 40 and truelove <= 50:
print(f"Your score is {truelove}, you are alright together")
else:
print(f"Your score is {truelove}")
解决方案
truelove <= 10 and truelove >= 90
将始终给出false
并且不通过此 if 语句。
为了能够运行它,您可以尝试。
truelove >= 10 and truelove <= 90
编辑:我看到你的elif
陈述永远不会奏效,因为第一个if
陈述范围更广。所以翻转 if 语句将解决它。
if truelove >= 40 and truelove <= 50:
print(f"Your score is {truelove}, you are alright together")
elif truelove >= 10 and truelove >= 90:
print(f"Your score is {truelove}, you go together like coke and mentos")
else:
print(f"Your score is {truelove}")```
推荐阅读
- windows - 如何使用 PoweShell 设置快捷方式的高级属性,如字体大小(如果是控制台应用程序)?
- html - 当我在下拉列表中选择第二个选项时。我想提示第二个标签内容,
- python - 使用 if 语句过滤列以计算新的单元格值。值错误:
- elasticsearch - 启动 Logstash,将其连接到 kafka 生产者
- python - 如何创建由骰子损失和焦点损失组成的混合损失 [Python]
- python - 为什么python说这个字符串是Type None
- css - 为什么 Prime React 下拉菜单无法正常工作?
- javascript - 如何从另一个 HTML 页面获取元素
- java - 更改方向时Android应用程序崩溃
- mysql - 如何解决此错误?我必须将数据添加到现有数据中