Callum
Callum

Reputation: 3

Variables not defined

When I try to run it I receive the outputs that word is not defined and that game() has an issue. Could I get some help?

def game():
  word = input("Gimme a word: ")
  if word.isalpha():
    print("It's all letters")
    word = answer
  else:
    print('nope')
    game()

game()
print(answer)

Upvotes: 0

Views: 78

Answers (2)

Zakir Hussain
Zakir Hussain

Reputation: 384

It is because you don't have variable answer

So declare a global variable answer

answer = '' 
def game():
  word = input("Gimme a word: ")
  if word.isalpha():
    print("It's all letters")
    word = answer
  else:
    print('nope')
    game()

game()
print(answer)

Upvotes: 0

Bill Elim
Bill Elim

Reputation: 36

I don't know what do you want this code to do, so I'll assume that it will print the answer if you enter a string that only contained letters

answer = 'asdasdasdasdanswer'

def game():
  while True:
    word = input("Gimme a word: ")
    if word.isalpha():
      print("It's all letters")
      return answer
    else :
      print("nope")

print(game())

or if you want to edit the value of word

def game():
  while True:
    word = input("Gimme a word: ")
    if word.isalpha():
      print("It's all letters")
      return word
    else :
      print("nope")

answer = game()
#do something with answer
print(answer)

Upvotes: 1

Related Questions