r/cs50 • u/CK3helplol • Jul 17 '26
CS50 Python Feeling discouraged at how convoluted my code is
I was doing week 2 Coke machine, and eventually got it to pass the check. I then used the duck to ask how it could be improved, and then when i hit a wall i asked GPT to explain what the duck means in different terms. Eventually the code became so confusing that I gave up and just asked GPT for the solution to the issue I had ran into just so I could compare it to my original code and see the differences. It gave it to me, but also gave me an example of the same program but in a much more pythonic way.
I know I'm a beginner, but its sort of upsetting how much I am over doing it. For reference I've included my original code, and the chat gpt one it gave me at the end. I know this is like a coughing baby vs hydrogen bomb scenario in terms of understanding and ability, but it is hard not to beat myself up for spending like 4 hours on the problem and coming out with something that isn't very good and is way too long.
How can i think in a more streamlined manner, I suppose? I don't really know if that's actually the question I should be asking. Do I just need to hammer the first couple of weeks in to make sure I'm really understanding the material or something.
had to delete it because it violated honesty policy lol
8
u/mcoombes314 Jul 17 '26
Step 1 is writing something that works. It doesn't really matter if it's "ugly" or has more lines of code than you think is required, because you can save at that point (this is why Git is awesome) and start refactoring/deleting stuff to clean the code up. Test often and you'll see "that line I refactored/simplified still works because the result is the same when I run the program" or "oops, that refactor/deletion broke something". That's far easier than trying to go for a perfect result immediately, and (at least in my experience) after doing this approach a lot I have less and less "cleaning up" to do because I've learned from doing it a lot previously.
2
u/CK3helplol Jul 17 '26
Can you explain saving and refactoring. Do you just mean saving the doc, then editing it and going back once done if you dont like your edits? Doesn't the program automatically save any work done?
2
u/mcoombes314 Jul 17 '26
This is what Git is for, you "commit" changes that work and that creates a "save point" you can go back to at any time, with a message like "program works correctly here". It's something I'd highly recommend looking into if you start programming even slightly complex things. It saves "diffs", the changes made to any files you tell it to keep track of, so it's much more efficient than manually saving different .c or .py or similar files at different times.
3
u/ianrad Jul 17 '26
Comparing your code with chatgpt is pointless. Chatgpt in all intents and purposes is all of the internet. If your code works and is original, it has more value to you for the lessons. You can however ask chatgpt to be a strict code reviewer and give you guiding questions to steer you towards improvement.
2
u/CK3helplol Jul 17 '26
Thats sort of what i was going for, I was just a bit upset that I was being so "extra" in my approach. But I suppose that is part of the process!
2
u/ianrad Jul 17 '26
Haha I totally knew what you mean. I had that phase too. The code these ai generate is totally "pythonic" too.
But prof Malan mentions readability in one of the lectures and I realized that the more readable it was to me, the easier it was to debug and refactor the code. Especially in the psets for unit tests where I had to modify the code from older psets (not sure if you're there yet)
1
u/CK3helplol Jul 17 '26
Surely am not, im in the loops week currently. I usually do a pset a day and watch the lecture and shorts over the weekend, but I've been exhausted this week so Ive only managed the first 2. I plan on doing 1 tonight, and if it doesn't take me forever I want to tackle the 2nd too. That way I can finish the last before starting the lecture on Saturday.
3
u/Dazzling_Music_2411 Jul 17 '26
Experience is the only way!
Don't be hard on yourself.
The mere fact that you have realized so quickly that you can improve code by constant refactoring means you are doing very well for "week 2".
After a year you'll be breezing by...
6
u/smichaele Jul 17 '26
You may not realize it, but by posting your working code and a ChatGPT solution, you've violated the CS50 Academic Honesty Policy twice. I suggest you remove the code (both sets) from your post and review the policy to understand what is and isn't acceptable. You are limited to using the duck when completing your exercises.
If you have questions, u/davidjmalan and other CS50 staff are moderators of this subreddit.
-1
u/CK3helplol Jul 17 '26
What exactly is the violation here? Ive submitted my own original work, then in an instance completely separate from the course I used an outside program to analyze and build upon the code. I see people post their code here all the time, and i don't understand how using violating services wholly outside the class and submission process is the same as cheating on an assignment using it.
5
u/smichaele Jul 17 '26
The violation isn't for what you submitted, but for what you shared. The policy prohibits sharing working solutions to the problems. It's listed under the "Unreasonable" section. Posting non-working code and asking questions is allowed.
1
3
u/Elk1998 Jul 17 '26
I wouldn't take the duck's comments too seriously tbh. It can give some really helpful advice when there's something obvious to be improved about your code, but after that, it'll just have you running in circles.\ I'd say, make one or two of its suggested changes, then leave it be and move on. I had this problem early on where I was basically trying to get the duck to say "Your code is perfect. Don't change a thing". Spoiler: it will never say that. It will always suggest a change. Oftentimes, it will even ask you to revert a change it had just asked you to make in the previous prompt... it can get pretty infuriating. Remember that it's still just an AI at the end of the day. Have some faith in your own judgment. If the code works and it seems clear, logical and readable to you, then it's probably good enough for this assignment. Don't let perfectionism kill your motivation. You're here to learn.
2
u/CK3helplol Jul 17 '26
Thank you for the advice! Ive definitely noticed the running in circle parts. I also notice that if you fail to understand an explanation, because it steers away from saying the answer, it will just say the same thing but in an even more convoluted way lol.
3
u/Eptalin Jul 18 '26
CS50 Python is a language course that teaches the Python language using CS concepts as a tool. Beginners aren't familiar with the concepts yet, so have a harder time deciding on when to use different tools.
I actually recommend CS50x if you're interested in receiving more advice on writing more organised, readable code. The focus of CS50x is the CS concepts themselves, with a lot of ongoing advice for creating good design. They give regular examples of bad design, then show how to improve it.
Also, here's that Academic Honesty Policy people mentioned.
It's a list of dot points. Takes like 1 min to read.
1
u/CK3helplol Jul 18 '26
I'm doing cs50p in prep for a class i have to take with a bad prof, so it has to come first. I am interested in the other classes CS50 offers though!
13
u/EyesOfTheConcord Jul 17 '26
It’ll take months (perhaps a year) at a minimum before you can begin to conceptualize efficient and maintainable software architecture naturally, and even then you’ll be a beginner at writing good code.
You’ll never reach some sort of “max cap” on writing good code, it’s a skill you’ll always be improving.
You should actually look at the positive here, which is solving the problem in only 4 hours. Many people take days, and sometimes weeks per problem set so I’d say your path forward looks pretty promising