Reputation: 45
Why doesn't this string handling work?
When you input "hectare"
, the full answer, it should print "+2"
. When only "hect"
is correct and the next 3 letters are incorrect the print should be "+1"
.
At the moment I don't get a print when only getting "hect"
correct.
answer = "hectare"
answerlen = int(len(answer)/2)
test = str(input("enter it"))
if test == answer:
print("+2")
else:
if test == answer[0:answerlen]:
print("+1")
Upvotes: 0
Views: 64
Reputation: 122032
You are comparing the whole of test
to the start of answer
. Instead, try:
if test[:answerlen] == answer[:answerlen]:
Note that 0
is the default start for a slice.
Also, you can simplify using integer division:
answerlen = len(answer) // 2
and elif
if test == answer:
print("+2")
elif test[:answerlen] == answer[:answerlen]:
print("+1")
and startswith
:
elif answer.startswith(test[:answerlen]):
Upvotes: 3