简体   繁体   中英

Python Int object not callable

Please help! I don't understand the error here. Why do I get an error saying: "'int' object is not callable" when I type a number other than 0, 1 or 2? Instead, it's suppose to print "You have entered an incorrect number, please try again" and go back to asking the question.

Second Question: Also how can I change the code in a way that even if I type letter characters, it won't give me the Value Error and continue re-asking the question? Thank you!

def player_action():        
    player_action = int(input("Enter 0 to stay, 1 to go Up, or 2 to go Down: "))

    if player_action == 0:
        print ("Thank You, you chose to stay")


    if player_action == 1:
        print ("Thank You, you chose to go up")

    if player_action == 2:
        print ("Thank You, you chose to go down")

    else:
        print ("You have entered an incorrect number, please try again")
        player_action()

player_action()

The first answer to your question has been answered by Pedro, but as for the second answer, a try except statement should solve this:

EDIT: Yeah sorry, I messed it up a little... There are better answers but I thought I should take the time to fix this

def player_action():
    try:
        player_action_input = int(input("Enter 0 to stay, 1 to go Up, or 2 to go Down: "))
    except ValueError:
        print("Non valid value") # or somehting akin
        player_action()
    if player_action_input == 0:
        print ("Thank You, you chose to stay")
    elif player_action_input == 1:
        print ("Thank You, you chose to go up")
    elif player_action_input == 2:
        print ("Thank You, you chose to go down")
    else:
        print ("You have entered an incorrect number, please try again")
            player_action()

player_action()

You should change the variable name as @Pedro Lobito suggest, use a while loop as @Craig suggested, and you can also include the try...except statement, but not the way @polarisfox64 done it as he had placed it in the wrong location.

Here's the complete version for your reference:

def player_action():    
    while True:   
        try:
            user_input = int(input("Enter 0 to stay, 1 to go Up, or 2 to go Down: "))
        except ValueError:
            print('not a number')
            continue

        if user_input == 0:
            print ("Thank You, you chose to stay")          

        if user_input == 1:
            print ("Thank You, you chose to go up")

        if user_input == 2:
            print ("Thank You, you chose to go down")

        else:
            print ("You have entered an incorrect number, please try again")
            continue
        break

player_action()

Just change the variable name player_action to a diff name of the function, ie:

def player_action():
    user_input = int(input("Enter 0 to stay, 1 to go Up, or 2 to go Down: "))
    if user_input == 0:
        print ("Thank You, you chose to stay")
    elif user_input == 1:
        print ("Thank You, you chose to go up")
    elif user_input == 2:
        print ("Thank You, you chose to go down")
    else:
        print ("You have entered an incorrect number, please try again")
        player_action()

player_action()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM