0

I'm making this guessing game to have the user guess a random word from a list. I'm confused about how to create if statements where the user's input is greater than or less than the index of the randomly selected word from the fruit list.

import random

fruit = ["blueberry", "grape", "apple", "mango", "watermellon"]
fruits = random.choice(fruit) 

guess = input("Guess a fruit: blueberry, grape, apple, mango, or watermellon: \n")

while guess != fruits: 
  if guess < fruits.index:
    print("-You've guessed low, try again")
  elif guess > fruits.index:
    print("-You've guessed high, try again")
  elif guess == fruits:
    print("You guessed the right number!")
Spencer
  • 3
  • 1
  • `random.choice()` just returns a value from the list. Both `guess` and `fruits` are _strings_. `fruits.index` isn't what you think it is. You'll need to look up the index in the `fruit` list using `fruit.index(fruits)` (or `fruit.index(guess)`). – gen_Eric Jul 14 '21 at 17:51
  • 1
    Also, I suggest naming your list `fruits` and the randomly selected item `fruit`. – gen_Eric Jul 14 '21 at 17:52

3 Answers3

2

You want to compare the index of the input to the index of the answer. Try it like this:

while guess != fruits: 
    if fruit.index(guess) < fruit.index(fruits):
        print("-You've guessed low, try again")
        guess = input("Guess a fruit: blueberry, grape, apple, mango, or watermellon: \n")
        
    elif fruit.index(guess) > fruit.index(fruits):
        print("-You've guessed high, try again")
        guess = input("Guess a fruit: blueberry, grape, apple, mango, or watermellon: \n")

#the loop breaks when guess==fruits and enters the else block
else:
    print("You guessed the right number!")
not_speshal
  • 22,093
  • 2
  • 15
  • 30
2

First, you need to fix your variable names: fruits should refer to a group of fruits (names) and fruit should refer to a single fruit (name).

fruits = ["blueberry", "grape", "apple", "mango", "watermellon"]
fruit = random.choice(fruits) 

You can also replace the random.choice to a random.randrange to get the index of a random fruit in the fruits:

fruits = ["blueberry", "grape", "apple", "mango", "watermellon"]
i_fruit = random.randrange(len(fruits))
fruit = fruits[i_fruit]

You can now use a loop to check the user input. I'll be using an infinite loop to avoid repeating code:

while True:
    guess = input("Guess a fruit: blueberry, grape, apple, mango, or watermellon: \n")

    # If the user guessed right
    if guess == fruit:
        print("You guessed the right fruit!")
        # Exit the loop
        break

    # Find the guess index in the fruits list
    i_guess = fruits.index(guess)
    
    # Compare the indexes
    if i_guess < i_fruit:
        print("-You've guessed low, try again")
    else:
        print("-You've guessed high, try again")
enzo
  • 9,861
  • 3
  • 15
  • 38
0

I would suggest you to do this:

import random

fruits = ["blueberry", "grape", "apple", "mango", "watermellon"]
fruit = random.choice(fruits)
fi = fruits.index(fruit)

def ask():
        global gi
        guess=input("Guess a fruit: blueberry, grape, apple, mango, or watermellon: \n")
        if guess not in fruits:
            print("You have entered wrong word would you mind to enter right word again")
            guess=ask()
        else:
            gi=fruits.index(guess)
            return 

while True:
  ask()
  if gi<fi:
    print("-You've guessed low, try again")
  elif gi>fi:
    print("-You've guessed high, try again")
  else :
    print(f"You guessed the right answer, Yes Answer is {fruit}!")
    break

This is also going to check if guess is in fruits or not because by doing this your program will not encounter an error if user typed wrong word or word that is not in fruits and additionally your user can type again if typed word is wrong.

imxitiz
  • 3,920
  • 3
  • 9
  • 33
  • It's more efficient to do `guessIndex = fruit.index(guess); fruitIndex = fruit.index(fruits)` and then compare `guessIndex` and `fruitIndex` instead of calling `.index()` for every comparison. – Pranav Hosangadi Jul 14 '21 at 18:01
  • @Spencer Would you like to check my answer? – imxitiz Jul 14 '21 at 18:40
  • I had tried to do like you said @PranavHosangadi but it is encountering error if user typed wrong word. So, at last I ended here. Do you think it is "_**efficient**_" ? – imxitiz Jul 14 '21 at 18:43