-1

I get the error 'TypeError: unorderable types: type() > int()' and i am not sure how to fix it.

    print ("Hello user, and welcome to the game.")
    print ("As you are a new user you will need to register your details, you will be asked to enter your age, gender, \nemail address and player name.")

    age = input("Please enter your age: ")
    max_age = (122)

    while type(age) != int and type(age) > max_age:
        print ("Sorry what you have entered is invalid. Please try again")
        age_attempt = input("Please enter your age: ")
        break

    gender = input("Please enter your gender. Male/Female: ")
    while type(gender) != Male or Female:
        print ("Sorry what you have entered is invalid. Please try again")
        gender_attempt  = input("Please enter your gender. Male/Female: ")
        break
  • 2
    in your code is so much wrong, `input` returns always strings, `or` does not work as you think, literal strings must be in quotes, ... – Daniel Aug 27 '15 at 10:29
  • Preparing an minimal, complete and verifiable example (http://stackoverflow.com/help/mcve) is useful for several reasons. One is that it would reduce the code you'll have to post, but also it will provide you with insights about the fault (and maybe figure it out yourself). The code would reduce to `type(whatever_you_input) > 122`, which is wrong because in python3 you can't compare a type object and a number (in python2 it is possible, but don't make much sense anyway). – skyking Aug 27 '15 at 11:16

3 Answers3

2

You are trying to compare a type with an integer here:

while type(age) != int and type(age) > max_age:

Remove the type() call from the > comparison:

while type(age) != int and age > max_age:

The type(age) != int call will always fail, because the input() function always returns objects of type str instead. You need to convert your input value to an integer first, perhaps using try...except to catch errors:

try:
    age = int(age)
except ValueError:
    # not a string that can be converted

See Asking the user for input until they give a valid response for a canonical question and answer on that subject.

You make the same mistake with your other test:

while type(gender) != Male or Female:

You need to test gender against two different values, so use a membership test:

while gender not in {'Male', 'Female'}:

See How do I test one variable against multiple values? why gender != 'Male' or 'Female' doesn't work.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

Small adding to @martin's code

Process:

1.Get user age input check whether they are int and lesser then max limit

2.Get Gender and check if they are either male or female if not prompt again else finish

Code:

print ("Hello user, and welcome to the game.\n")
print ("As you are a new user you will need to register your details, you will be asked to enter your age, gender, \nemail address and player name.\n")

max_age = 122

while True:  #Loops while break condition is meet .That is if no break is there it will become inifinite 
    try:
        age = int(input("\nPlease enter your age: "))
    except ValueError:
        print ("Sorry what you have entered is invalid. Please try again")
        continue    
    if age<max_age:        
        break # exit loop when max age is high then input
    print ("Sorry what you have entered is invalid. Please try again")  

gender = input("Please enter your gender. Male/Female: ")

while gender.lower().strip() not in {"male","female"}:

    print ("Sorry what you have entered is invalid. Please try again")
    gender  = input("Please enter your gender. Male/Female: ") # Note same variable is used here not different variable

Notes:

type is used find the type of the given argument .For more on type see this so question

>>> i = 123
>>> type(i)
<type 'int'>
>>> type(i) is int
True
>>> i = 123456789L
>>> type(i)
<type 'long'>
>>> type(i) is long
True
>>> i = 123.456
>>> type(i)
<type 'float'>
>>> type(i) is float
True
Community
  • 1
  • 1
The6thSense
  • 8,103
  • 8
  • 31
  • 65
-1

Here is a working example :

print ("Hello user, and welcome to the game.")
print ("As you are a new user you will need to register your details, you will be asked to enter your age, gender, \nemail address and player name.")

age = int(input("Please enter your age: "))
max_age = 122

while age>max_age:
    print ("Sorry what you have entered is invalid. Please try again")
    age_attempt = int(input("Please enter your age: "))
    break

gender = input("Please enter your gender. Male/Female: ")
while type(gender) != Male or Female:
    print ("Sorry what you have entered is invalid. Please try again")
    gender_attempt  = input("Please enter your gender. Male/Female: ")
    break
Gilles-Antoine Nys
  • 1,481
  • 16
  • 21
ishika
  • 1
  • 1
  • 6