Python初学者:if语句不返回print函数

2024-10-05 10:15:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近试着用这段代码做一个爱情计算器。然而,当一个名字有超过11个共同的“爱”或“真”字符时,它不会返回正确的语句。例如,如果因为'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}")

Tags: andinputyournamesiscount语句else
1条回答
网友
1楼 · 发布于 2024-10-05 10:15:29
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}")```

相关问题 更多 >

    热门问题