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
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 288 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  Commas issue in variable ddahlman 6 453 Apr-05-2024, 03:45 PM
Last Post: deanhystad
  Variable Explorer in spyder driesdep 1 229 Apr-02-2024, 06:50 AM
Last Post: paul18fr
  Mediapipe. Not picking up second variable stevolution2024 1 194 Mar-31-2024, 05:56 PM
Last Post: stevolution2024
Question Variable not defined even though it is CoderMerv 3 291 Mar-28-2024, 02:13 PM
Last Post: Larz60+
  optimum chess endgame with D=3 pieces doesn't give an exact moves_to_mate variable max22 1 278 Mar-21-2024, 09:31 PM
Last Post: max22
  unbounded variable akbarza 3 501 Feb-07-2024, 03:51 PM
Last Post: deanhystad
  Variable for the value element in the index function?? Learner1 8 656 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable definitions inside loop / could be better? gugarciap 2 445 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  working directory if using windows path-variable chitarup 2 740 Nov-28-2023, 11:36 PM
Last Post: chitarup

Forum Jump:

User Panel Messages

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