+ 1

I cant get this code to work. I've looked around and cant find anything that will help but every time i run it i get an error

print('Now for part 2 of the guessing game!') print('I am thinking of a fruit but you need to guess which one!') fruit = ['Grape', 'Blueberry', 'Banana', 'Cherry', 'Peach', 'Lemon', 'Strawberry', 'Lime', 'Plum', 'Honeydew', 'Nectarine', 'Blackberry', 'Watermelon', 'Cantalope', 'Raspberry', 'Dragonfruit'] secretFruit = random.choice(fruit) for tries in range(1, 5): print('Please guess a fruit') fruitGuess = str(input()) if fruitGuess == secretFruit: print('Wow you nailed it! You, '+name+' are a natural psychic!') elif tries == 1: print('You have 4 tries left! Guess again') elif tries == 2: print('Thats two tries down, three to go!') elif tries == 3: print('Well thats three. I hope your last two guesses are better than your first three!') elif tries == 4: print('You have one guess left! Make it count') elif tries == 5: print('Im sorry, the fruit in my head was '+secretFruit+'. Better luck next time!') else: print('Im sorry, the fuit was '+secretFruit+'. Better luck next time!')

23rd May 2017, 11:18 PM
Allan Stewart
Allan Stewart - avatar
2 ответов
+ 3
In the code you are using a variable called 'name'. You did not define it. Maybe that's what causing the error. But for us to be sure you need to tell us what is the error.
24th May 2017, 12:09 AM
Ulisses Cruz
Ulisses Cruz - avatar
+ 2
import random fruit = ['Grape', 'Blueberry', 'Banana', 'Cherry', 'Peach', 'Lemon', 'Strawberry', 'Lime', 'Plum', 'Honeydew', 'Nectarine', 'Blackberry', 'Watermelon', 'Cantalope', 'Raspberry', 'Dragonfruit'] print('Please enter a name:') name = str(input()) secretFruit = random.choice(fruit) for tries in range(1, 6): print('Please guess a fruit') fruitGuess = str(input()) if fruitGuess == secretFruit: print('Wow you nailed it! You, '+name+' are a natural psychic!') break elif tries == 1: print('You have 4 tries left! Guess again') elif tries == 2: print('Thats two tries down, three to go!') elif tries == 3: print('Well thats three. I hope your last two guesses are better than your first three!') elif tries == 4: print('You have one guess left! Make it count') elif tries == 5: print('Im sorry, the fruit in my head was '+secretFruit+'. Better luck next time!') else: print('Im sorry, the fuit was '+secretFruit+'. Better luck next time!') Looks like you just forgot to define the variable, '+name+'.
24th May 2017, 1:55 PM
Aaron Jones
Aaron Jones - avatar