I was looking for a program to do the elfish instances. Then I found this javascript code on the web.
var containsE;
var containsL;
var containsF;
function elfish(str){
var checkLetter = str[str.length - 1];
if (checkLetter === "e"){
containsE = true;
}
else if (checkLetter === "l"){
containsL = true;
}
else if (checkLetter === "f"){
containsF = true;
}
// base case
if (str.length === 0)
if (containsE && containsL && containsF){
return true;
}
else {
return false;
}
// if not base case
return elfish(str.slice(0, str.length - 1));
}
elfish("whiteleaf");
I wonder if it is possible to guide me to convert the code into python with an explanation? the python version is 2.73
-
Could you explain what the desired output of the code should be?Simon Crane– Simon Crane2019年10月02日 13:35:53 +00:00Commented Oct 2, 2019 at 13:35
-
2Try it yourself. If you have a specific question, you can ask here.Michael Butscher– Michael Butscher2019年10月02日 13:36:25 +00:00Commented Oct 2, 2019 at 13:36
-
Hi, so for the output, if the user enter the word waffles then it is a elfishfulniz– fulniz2019年10月02日 14:31:10 +00:00Commented Oct 2, 2019 at 14:31
1 Answer 1
I think there is nothing special to explain here. Python is very self-explaining in your case. To keep it readable i added parenthesis in the if statement. You could just leave them:
while True:
# type in which word you want
s = input()
# if some letter in your string was found
if ('e' in s) and ('l' in s) and ('f' in s):
print('true')
else:
print('false')
default