# [SOLVED] Guess my number help (python)

its me again :,D so im having trouble on guess my number. my current code shows up with an error because in the “hero.say(word)”, word is apparently undefined? i dont know, im pretty confused

``````# You need to guess the number from 1 to 999 (0 < n < 1000).
# You have 10 attempts!
# For each attempt Riddler will say if the number is less or greater.
# If the guessed number is less than your try, then a munchkin ogre appears.
# Else a scout ogre appears.

lowestPossible = 1
highestPossible = 999

while True:
# You need to defeat the enemy before the next attempt.
enemy = hero.findNearest(hero.findEnemies())
if enemy:
# "scout" is 'greater', "munchkin" is less.
# Prepare for the next attempt and wipe out the ogre.
if enemy.type == "scout":
highestPossible = word
word = (word + lowestPossible) / 2
if enemy.type == "munchkin":
lowestPossible = word
word = (word + highestPossible) / 2
hero.attack(enemy)
else:
# Make your next attempt and say it.
hero.say(word)
``````
1 Like

you need to

``````if enemy:
hero.attack(enemy)
``````

``````hero.attack(enemy)
``````

The “word” variable is only defined if there is an enemy, and the hero only says the word if there is no enemy.

so ive changed my code to this, and im still not sure why it isnt working. also im not really sure what dividing by two does? if thats part of the solution, i might need someone to explain it to me.

``````# You need to guess the number from 1 to 999 (0 < n < 1000).
# You have 10 attempts!
# For each attempt Riddler will say if the number is less or greater.
# If the guessed number is less than your try, then a munchkin ogre appears.
# Else a scout ogre appears.

lowestPossible = 1
highestPossible = 999

while True:
# You need to defeat the enemy before the next attempt.
enemy = hero.findNearest(hero.findEnemies())
if enemy:
# "scout" is 'greater', "munchkin" is less.
# Prepare for the next attempt and wipe out the ogre.
if enemy.type == "scout":
highestPossible = word
word = (word + lowestPossible) / 2
hero.attack(enemy)
if enemy.type == "munchkin":
lowestPossible = word
word = (word + highestPossible) / 2
hero.attack(enemy)
else:
# Make your next attempt and say it.
hero.say(word)
``````

ok now im not even sure if the level is made right, this is the solution my teacher provided and it doesnt work

(i removed the solution, no cheating allowed <3)

edit: nevermind, it worked, i just got some of the parentheses wrong