Jump to content

Python quiz 💯 until Gpt 😆

Ripred

Alrighty,folks, im puzzled...so I made up a solution to a quiz in my Python class but after I already ran the class test program and it came back 100% I thought for kicks id dump it into chatGPT to get a quick opinion, it said it was fine but could be tightened up, gave some examples and such but long story short I tried one of them that looked good but then I re-ran the test program and boom 3 assertion failed, and why I'm making this thread is to ask if the more experienced can clear up why because up against my current knowledge it looks like it should work... Okay firstly, for context, the quiz was to take an input of some fake file name, with an extension, .gif, .PDF, and return the file type, so here's the original before adding GPT suggestion

 

 

 

def main():
    get_file=input("File name: ").split(".")
    get_file[-1]=get_file[-1].lower().strip()
    if get_file[-1] in extensions:
        return((extensions.index(get_file[-1])))
    else:
        pass

extensions=("gif","jpg","jpeg","png","pdf","txt","zip",)

types = ("image/gif",
         "image/jpeg",
         "image/jpeg",
         "image/png",
         "application/pdf",
         "text/plain",
         "application/zip",)

try:
    print(types[main()])
except:
    print("application/octet-stream")

 

Alright so GPT said my return statement in the function could be changed to return the types immediately like this 

return types[extensions.index(get_file[-1])]

Then my print only needs to be print(main())

Which seemed like an okay idea

But throws assertion errors, can't assert none type and what not but it was three assertion out of 17 😆 

                          Ryzen 5800X3D(Because who doesn't like a phat stack of cache?) GPU - 7700Xt

                                                           X470 Strix f gaming, 32GB Corsair vengeance, WD Blue 500GB NVME-WD Blue2TB HDD, 700watts EVGA Br

 ~Extra L3 cache is exciting, every time you load up a new game or program you never know what your going to get, will it perform like a 5700x or are we beating the 14900k today? 😅~

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Ripred said:

Alright so GPT said my return statement in the function could be changed to return the types immediately like this 

return types[extensions.index(get_file[-1])]

Then my print only needs to be print(main())

Which seemed like an okay idea

But throws assertion errors, can't assert none type and what not but it was three assertion out of 17 😆 

Uhm, silly question...but did you happen to copy paste the chatGPT code itself?  Or did you just make the modification like you mentioned here?

 

I'll mentioned what is happening, but also at the same time I wouldn't be surprised if chatGPT switched around a few of the strings in your extensions etc (ontop of what I mentioned...I've had chatGPT hallucinate a few things)

 

Anyways, when you hit something like this it is important to look at the assertions it is failing so you can start hypothesizing why it might be messing up

 

So consider the case

myperfectfile.bmp

 

Your code will look up bmp, not find it in extensions, pass (and thus returning a none type) and the trying to use print(types[none])

This will throw an exception, so your code reverts to the exception handling

 

Now for the chatGPT

Looks up bmp, not find it in the extension, pass (thus returning none) and then doing print(none)

Print(None) is valid

 

So that's why you aren't seeing the same results.

 

For the chatGPT version instead of pass you would return the "application/octet-stream"

 

On a side note, this is how you could make it slightly more readable/maintainable

def get_extension():
    get_file = input("File name: ").split(".")
    return get_file[-1].lower().strip()


types = {"gif":"image/gif",
         "jpg":"image/jpg",
         "jpeg":"image/jpeg",
         "png":"image/png",
         "pdf":"application/pdf",
         "txt":"text/plain",
         "zip":"application/zip"}

try:
    print(types[get_extension()])
except:
    print("application/octet-stream")

 

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, wanderingfool2 said:

Uhm, silly question...but did you happen to copy paste the chatGPT code itself?  Or did you just make the modification like you mentioned here?

 

I'll mentioned what is happening, but also at the same time I wouldn't be surprised if chatGPT switched around a few of the strings in your extensions etc (ontop of what I mentioned...I've had chatGPT hallucinate a few things)

 

Anyways, when you hit something like this it is important to look at the assertions it is failing so you can start hypothesizing why it might be messing up

 

So consider the case

myperfectfile.bmp

 

Your code will look up bmp, not find it in extensions, pass (and thus returning a none type) and the trying to use print(types[none])

This will throw an exception, so your code reverts to the exception handling

 

Now for the chatGPT

Looks up bmp, not find it in the extension, pass (thus returning none) and then doing print(none)

Print(None) is valid

 

So that's why you aren't seeing the same results.

 

For the chatGPT version instead of pass you would return the "application/octet-stream"

 

On a side note, this is how you could make it slightly more readable/maintainable

def get_extension():
    get_file = input("File name: ").split(".")
    return get_file[-1].lower().strip()


types = {"gif":"image/gif",
         "jpg":"image/jpg",
         "jpeg":"image/jpeg",
         "png":"image/png",
         "pdf":"application/pdf",
         "txt":"text/plain",
         "zip":"application/zip"}

try:
    print(types[get_extension()])
except:
    print("application/octet-stream")

 

Ah so that's where these return None types are coming from, so my pass seems to be a weak point, and if i change it to instead return something else it should work?🤔, thanks. I should be able to sort that out now, yea my first instinct was to use a dictionary containing key:values but this quiz is from earlier weeks in the course before dictionaries are covered and this part is under the topic - conditionals, I'm back tracking to earlier weeks to finish assignments, and trying to restrict myself to what was covered up to the point of each topic, this online class doesn't enforce a time limit to submit work aslong as its in before the end of the course so I tend to skip ahead when I get into the material...

~Thanks

                          Ryzen 5800X3D(Because who doesn't like a phat stack of cache?) GPU - 7700Xt

                                                           X470 Strix f gaming, 32GB Corsair vengeance, WD Blue 500GB NVME-WD Blue2TB HDD, 700watts EVGA Br

 ~Extra L3 cache is exciting, every time you load up a new game or program you never know what your going to get, will it perform like a 5700x or are we beating the 14900k today? 😅~

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Ripred said:

Ah so that's where these return None types are coming from, so my pass seems to be a weak point, and if i change it to instead return something else it should work?🤔, thanks. I should be able to sort that out now, yea my first instinct was to use a dictionary containing key:values but this quiz is from earlier weeks in the course before dictionaries are covered and this part is under the topic - conditionals, I'm back tracking to earlier weeks to finish assignments, and trying to restrict myself to what was covered up to the point of each topic, this online class doesn't enforce a time limit to submit work aslong as its in before the end of the course so I tend to skip ahead when I get into the material...

~Thanks

So yea, effectively if you don't return anything it returns none (actually the way the code was written, if you just ignored the else/pass statement it has the same effect)

 

The issue that you would have though with the way things are written is effectively "what would you return"?  Ironically returning None is the proper solution as per your code.  For the chatGPT yea, returning the octet-stream is the correct way then.

 

As a comment though, try avoiding using generic naming for functions.  Try naming it so that at a glance someone could guess what the function may be doing

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

20 hours ago, wanderingfool2 said:

So yea, effectively if you don't return anything it returns none (actually the way the code was written, if you just ignored the else/pass statement it has the same effect)

 

The issue that you would have though with the way things are written is effectively "what would you return"?  Ironically returning None is the proper solution as per your code.  For the chatGPT yea, returning the octet-stream is the correct way then.

 

As a comment though, try avoiding using generic naming for functions.  Try naming it so that at a glance someone could guess what the function may be doing

Thanks, this has been very helpful, Yea I would have named my function something a bit more clear but the professor teaching the course seems to want us to get into the habit of starting with a main function and calling main at the end

                          Ryzen 5800X3D(Because who doesn't like a phat stack of cache?) GPU - 7700Xt

                                                           X470 Strix f gaming, 32GB Corsair vengeance, WD Blue 500GB NVME-WD Blue2TB HDD, 700watts EVGA Br

 ~Extra L3 cache is exciting, every time you load up a new game or program you never know what your going to get, will it perform like a 5700x or are we beating the 14900k today? 😅~

Link to comment
Share on other sites

Link to post
Share on other sites

Just want to give a +1 to @wanderingfool2's suggestion for making things more readable/maintainable.

 

Your current implementation relies heavily on ensuring that the types and extensions are in the correct order, which is easy to mess up when modifying the code in the future.

 

@wanderingfool2's dictionary approach enforces that the extension string is used as the index when getting the type value.

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Ripred said:

Thanks, this has been very helpful, Yea I would have named my function something a bit more clear but the professor teaching the course seems to want us to get into the habit of starting with a main function and calling main at the end

Well having something that utilizes main is a good practice, but the way you utilized it defeats the general purpose of it.

def main():
    try:
        print(types[get_extension()])
    except:
        print("application/octet-stream")

def get_extension():
    get_file = input("File name: ").split(".")
    return get_file[-1].lower().strip()


types = {"gif":"image/gif",
         "jpg":"image/jpg",
         "jpeg":"image/jpeg",
         "png":"image/png",
         "pdf":"application/pdf",
         "txt":"text/plain",
         "zip":"application/zip"}

if __name__ == '__main__':
    main()

The purpose of utilizing main is so that you can easily find  what code gets run when you run the script.  It should almost boil down to running a single instance  of it, and not really use much code around it

 

Then of course you use __name__ == '__main__' to prevent the main from getting run if the script is imported instead of run

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, wanderingfool2 said:

The purpose of utilizing main is so that you can easily find  what code gets run when you run the script.  It should almost boil down to running a single instance  of it, and not really use much code around it

So I should think of the main function as sort of a junction point for the rest of the script?

                          Ryzen 5800X3D(Because who doesn't like a phat stack of cache?) GPU - 7700Xt

                                                           X470 Strix f gaming, 32GB Corsair vengeance, WD Blue 500GB NVME-WD Blue2TB HDD, 700watts EVGA Br

 ~Extra L3 cache is exciting, every time you load up a new game or program you never know what your going to get, will it perform like a 5700x or are we beating the 14900k today? 😅~

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, QuantumRand said:

 

Your current implementation relies heavily on ensuring that the types and extensions are in the correct order, which is easy to mess up when modifying the code in the future.

I realized that while writing it but this was in earlier weeks of the course and being that I'm back tracking I was trying to restrict myself to what was covered up until that point, the assignment requirements asked us to make assumptions in this instance, and was only concerned with the class grasping the concepts on conditionals with use of if, else, try, except, etc..

                          Ryzen 5800X3D(Because who doesn't like a phat stack of cache?) GPU - 7700Xt

                                                           X470 Strix f gaming, 32GB Corsair vengeance, WD Blue 500GB NVME-WD Blue2TB HDD, 700watts EVGA Br

 ~Extra L3 cache is exciting, every time you load up a new game or program you never know what your going to get, will it perform like a 5700x or are we beating the 14900k today? 😅~

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×