-1

I have written this code to ask for an input and then check if that word is english. If yes return true, if not return false.

def onlyEnglishLetters(word):
     word = (input("Enter word here: "))
 if word.isalpha():
    return true
return false

output = SyntaxError: 'return' outside function

james
  • 11
  • 4

1 Answers1

0

Here is an example of what you can do:

def onlyEnglishLetters():
    word = (input("Enter word here: "))
    if word.isalpha():
        return True
    else:
        return False

Your original code creates an error because the second return statement is outside of the function. By putting it in an else statement it becomes a part of the function.

You don't need to have 'word' as an argument in the function because you set the value with the input statement.

Also, this will not print the word 'True' or 'False', it only returns that value.

Edit:

If you want the code to print True or False then this is what you should do:

def onlyEnglishLetters():
    word = (input("Enter word here: "))
    if word.isalpha():
        print(True)
    else:
        print(False)

onlyEnglishLetters()
John Black
  • 18
  • 4
  • That seems to work, but then when I tried running the code their is no output, could there be any reason for this? – james Nov 19 '20 at 20:40
  • @james we have no idea what output you're expecting or how you're trying to get the output. If you want to be able to call `onlyEnglishLetters()` and have it output `True` or `False`, you have to put a `print()` _somewhere_. – Random Davis Nov 19 '20 at 20:45
  • Yes, it only returns the value of true or false. If you want it to print output the value change 'return True' to 'print("True")'. And you need to call the function after you define it. – John Black Nov 19 '20 at 20:46
  • Im expecting the code to ask me to input a word and the check if that word is true or false then return true or false – james Nov 19 '20 at 20:51
  • If you change the return statements to be print statements and then call the function (type `onlyEnglishLetters()` on a separate line after the function definition) it should work. – John Black Nov 19 '20 at 20:54