Python Forum
int(variable) not changing variable to an integer
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
int(variable) not changing variable to an integer
#1
For whatever reason the below code runs the command int(ans), but ans is not an integer when checked.
def intCheck(num):
    try:
        int(num)
        return True
    except:
        return False

def redo():
    ans = input('Input an integer: ')

    if intCheck(ans) == True:
        int(ans)
        print('Answer has been turned into an integer')
    else:
        print('You did not input an integer')

    if isinstance(ans, int):
        print('\nIt worked')
        redo()
    else:
        print("\nIt didn't work")
        redo()

redo()
This code also doesn't work, so the check can't be the problem.
ans = '1'
int(ans)

if ans == 1:
    print('It worked')
else:
    print("It didn't work")
The only other thing I can think of is ans might be a prohibited variable name, but I've changed it and the code still doesn't work.
I'm using python 3.6.3. Would appreciate any help if you know what's wrong, thanks. Smile
Reply
#2
If you want to turn ans into an integer, you need to write
ans = int(ans)
However, why not simply write
try:
    ans = int(ans)
except TypeError:
    print("You didn't enter an integer.")
else:
    print("You entered an integer.")
? This avoid a type-checking function.
Reply
#3
Thank you, completely forgot about that.

(Jan-11-2018, 10:28 AM)Gribouillis Wrote: However, why not simply write
try:
    ans = int(ans)
except TypeError:
    print("You didn't enter an integer.")
else:
    print("You entered an integer.")
? This avoid a type-checking function.

That works just as well, I've gotten into the habit of making a function in case I want to check multiple times. I just find it easier.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  assign value of index to variable zoor29 9 180 Apr-17-2026, 09:06 PM
Last Post: deanhystad
Question [SOLVED] Recommended way to declare global variable? Winfried 1 90 Mar-19-2026, 09:30 PM
Last Post: deanhystad
  link variable to exc blocks garbage collection Astrobert 1 747 Nov-19-2025, 05:03 AM
Last Post: Gribouillis
  variable changing types for error? Azdaghost 1 2,372 Sep-15-2025, 06:45 PM
Last Post: Gribouillis
  Functions: why use a variable "display" in a UDF as a Boolean MMartin71 3 1,138 May-26-2025, 05:55 AM
Last Post: DeaD_EyE
  PYTHONHOME Variable correct setting msetzerii 0 1,958 May-25-2025, 11:48 PM
Last Post: msetzerii
  how to get variable Azdaghost 3 1,146 Apr-23-2025, 07:43 PM
Last Post: deanhystad
  I trying to automate the Variable Logon button using the python code but I couldn't surendrasamudrala 0 744 Mar-07-2025, 05:02 AM
Last Post: surendrasamudrala
  not able to call the variable inside the if/elif function mareeswaran 3 1,532 Feb-09-2025, 04:27 PM
Last Post: mareeswaran
  creating arbitrary local variable names Skaperen 9 2,989 Sep-07-2024, 12:12 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020