Home > Software design >  If statement keeps giving same value regardless of user input
If statement keeps giving same value regardless of user input

Time:04-25

I am making a pokemon guessing game where the height and type are given, all works well thanks to stack overflow, but one thing, the height checker. The height checker is supposed to tell if the pokemon you guessed is taller or smaller than the pokemon that was chosen. Regardless of user input, the output is always the pokemon is a little taller than what you chose The code is below:

import random
life = 4

genone = ["Bulbasaur", "Ivysaur", "Venusaur", "Charmander ", "Charmeleon", "Charizard", "Squirtle", "Wartortle", "Blastoise", "Caterpie", "Metapod", "Butterfree", "Weedle", "Kakuna", "Beedrill", "Pidgey", "Pidgeotto", "Pidgeot", "Rattata", "Raticate", "Spearow", "Fearow", "Ekans", "Arbok", "Pikachu", "Raichu", "Sandshrew", "Sandslash", "Nidoran♀", "Nidorina", "Nidoqueen", "Nidoran♂", "Nidorino", "Nidoking", "Clefairy", "Clefable", "Vulpix", "Ninetales" "random poke"]
genonetypes = ["grass", "grass", "grass", "fire ", "fire", "fire/dragon", "water", "water", "water", "bug", "bug", "bug", "bug", "bug", "bug", "flying", "flying", "flying", "normal", "normal", "flying", "flying", "poison", "poison", "electric", "electric", "ground", "ground", "poison", "poison", "poison", "poison", "poison", "poison", "fairy", "fairy", "fire", "fire", "tester"]
genoneheight = ["0.7m", "1m", "2m", "0.6m", "1.1m", "1.7m", "0.5m", "1m", "1.6m", "0.3m", "0.7m", "1.1m", "0.3m", "0.6m", "1m", "0.3m", "1.1m", "1.5m", "0.3m", "0.3m", "0.7m","0.3m","1.2m","2m","3.5m","0.4m","0.8m","0.6m","1m","0.4m","0.8m", "1.3m","0.5m","0.9m","1.4m","0.6","1.3","0.6m","1.1", "4m"]
x, y, z = random.choice(list(zip(genone,genonetypes,genoneheight)))


while True: 
    print(x)
    print(z)
    print("The pokemon type is "   y   " and they are "   z   " tall (meters)")
    userin = input("enter the pokemon you think it is: ")

    if userin.lower() == x.lower():
        print("yes!!! great job you got it!!!11!!!!")
        break
        
    if userin != x:
        life = life-1
        print("that is incorrect you have "   str(life)   " lives left\n")
        print()
#height check
    if userin > z:
        print("the pokemon is a little taller than what you chose")
    if userin < z:
        print("the pokemon is a little smaller than what you chose")
        
    if life == 0:
        print("the pokemon was "   x   " .")
        print("better luck next time")
        break

is there an issue with the if statement or where it is placed?

CodePudding user response:

You may want to keep track of the variables userin and z. From your program, userin is supposed to be the "genone" (eg. Bulbasaur) while z is a string of height (eg. "0.7m"). So with conditional statement such as if userin < z, you are comparing two different things (eg. "genone" < "0.7m"). As for string comparison, '0' is smaller than 'g' so you will always have the pokemon is a little taller than what you chose printed.

What you will have to do is to, first, find the "genoneheight" of the "genone", and then compare this height with the "genoneheight" of the chosen pokemon.

A more convenient way to do this is to define a dictionary with genone as key, and genonetypes and genoneheight as values.

CodePudding user response:

userin is the name of the pokemon I suppose. userin is always going to be greater than z in this case.

What you must do is:

if genoneheight[genone.index(userin)] > z:
   print('Taller')
else:
   print('Shorter')

This gets the element in the index of index position of userin in genone from genoneheight. This may sound confusing. Please reply if it worked. Also, I suggest you to use float instead of str for the heights.

  • Related