Python Forum
Help with isinstance command (very simple code)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with isinstance command (very simple code)
#1
Hey!

I'm creating a simple function. The point would be to quit the code if n_files//n_pts is not an integer, however I can't get the condition to work: now it just prints 'Done', no matter if the result is an integer. I tried adding == True after the if command, but that doesn't do anything, so if anyone could help me on this one, I'd greatly appreciate it!

    
    print('Checking that n_files divided by n_pts is an integer...')
    if isinstance(n_files//n_pts, int):
        print('Done!')
    else:
        print('Not an integer, check the number of files in your folder')
        quit()
EDIT: Problem solved by changing command to if n_files//n_pts == int and using sys.exit() to quit the process!
Reply
#2
I'm pretty sure your problem is not solved. // is floor division, and the result is always an integer if the arguments are integers. If you want to know if n_files is evenly divisible by n_pts this should work:
if n_files % n_pts == 0:
    print('Done!')
Using sys.exit() to quit a process is usually a sign that you were too lazy to organize your code in a logical manner. When code is well organized you can get a feel for the flow just by looking at the white space. When code is sprinkled with "global" and "sys.exit()" you have to read the words.

For example:
def interesting_stuff()
   """Does all the interesting stuff"""

if n_files % n_pts == 0:
    interesting_stuff()
else:
    print('You messed up')
Reply
#3
Oh, thanks! Seems that my solution only worked if I manually entered the number of files, but when I asked for the code to calculate the number of files in a folder it wouldn't work. I took out sys.exit() as well. Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with simple code JacobSkinner 1 230 Mar-18-2024, 08:08 PM
Last Post: deanhystad
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 440 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,035 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  help me simple code result min and max number abrahimusmaximus 2 870 Nov-12-2022, 07:52 AM
Last Post: buran
  Simple encoding code ebolisa 3 1,400 Jun-18-2022, 10:59 AM
Last Post: deanhystad
  How to use a variable in linux command in python code? ilknurg 2 1,547 Mar-14-2022, 07:21 AM
Last Post: ndc85430
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 1,755 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  Simple code question about lambda and tuples JasPyt 7 3,238 Oct-04-2021, 05:18 PM
Last Post: snippsat
  My simple code don't works !! Nabi666 1 1,577 Sep-06-2021, 12:10 PM
Last Post: jefsummers
  Trying to understand how isinstance(values, collections.Iterable) work. quazirfan 7 4,096 Aug-10-2021, 08:10 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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