#isinstance help

12 messages · Page 1 of 1 (latest)

oblique flame
#
def milesToFeet(miles):
    return int(miles*5280)

def main():
    print("\n===============")
    print("Hike Calculator")
    print("===============\n")
    miles = input("enter number of miles here:\n")
    checkMiles = isinstance(miles,(int, float))
    print(checkMiles)
    if checkMiles == False:
        miles = input(f"\nplease enter a number\n")
        checkMiles = isinstance(miles, float) or isinstance(miles, int)
    milesToFeet(miles)
    feet = milesToFeet(miles)
    print(f"you have walked {feet} feet\n")
    choice = input("would you like to try again? (y/n)\n")
    if choice == ("y"):
        main()
    elif choice == ("n"):
        exit()
    else:
        while True:
            choice = input("please enter either y or n\n")
            if choice == ("y"):
                main()
            elif choice == ("n"):
                exit()
            else:
                continue
        

main()

ok so in the fifth line of the main function I tried using the "isinstance" function to check to see if the user entered a number or not but for some strange reason it is always coming out as false reguardless if i enter a number or not.
I know there is something else breaking right after that but this is the part im stuck in now lol

#

i tried using "isinstance(var, classinfo1 | classinfo2)" as well but same thing was happening, even when i kept only one argument

silver torrent
#

if not isinstance(miles, (int, float)):

oblique flame
silver torrent
#

Wait, hold on

#

I was looking at this part

if checkMiles == False:
        miles = input(f"\nplease enter a number\n")
        checkMiles = isinstance(miles, float) or isinstance(miles, int)
#

if checkMiles == False:

Can replace this with the one I gave

oblique flame
#

wait thats the second problem

#

the first one still keeps giving me False reguardless of what i put

#
  miles = input("enter number of miles here:\n")
    checkMiles = isinstance(miles,(int, float))
    print(checkMiles)

this

silver torrent
#

Ahh, input() always gives a string

#

So you'd have to parse that into an integer