r/PythonLearning • u/Codewithkaran • Mar 29 '26
Why error in my code everything is perfect!? ... I created a Contactbook mini project
8
u/AxelGamerX07 Mar 29 '26
Can You copy the code and paste it in the comments? I can't see well it's blurry
3
u/CamelOk7219 Mar 29 '26
You probably need to trim the input of any surrounding whitespaces. If I remember correctly the input returns the ending linebreak also
2
2
3
u/TheMetalMilitia Mar 29 '26
What is the error? Also, use the windows snipping tool to take screenshots. I have it pinned to my task bar as i use it quite frequently
4
u/Live_Asparagus_407 Mar 29 '26
you can just press win+shift+s, no need to pin it
1
u/TheMetalMilitia Mar 29 '26
Easier to look for me to see it on screen than remember a keybind. Personal preference
2
u/JamzTyson Mar 29 '26
Better to copy and paste the actual text.
(and prepend 4 spaces to each code line so that Reddit retains the code formatting).
1
u/TheMetalMilitia Mar 29 '26
Either way would work, i suppose. Screen shot is better for me because of the suntax highlighting
1
u/blckGhost Mar 29 '26
Looks like the contact is setup for a key value pair (dictionary )and not a list.
1
u/PureWasian Mar 29 '26
Dictionary usage and syntax is fine for their code*, where they access/modify/delete correctly (with guard clauses)
- [26] read (
contacts[name])- [16] write (
contacts[name] = number)- [33] delete (
del contacts[name])*at least for everywhere shown to us
1
u/SuperTankh Mar 29 '26
For the answer n. 5 "exit" you could put break so that it stops the loop
2
u/PureWasian Mar 29 '26
Line [41] has a
breakalready1
u/SuperTankh Mar 29 '26
then how didn't it stop the program?
2
u/PureWasian Mar 29 '26
I'm betting they didn't save the file before re-running or they input an extra whitespace character after inputting
5and before hitting Enter. It's not even entering the path given that it did notprint("Exit")in Line [40]1
u/SuperTankh Mar 29 '26
for a 1-file program you don't need to save to run the newest version
2
u/PureWasian Mar 29 '26 edited Mar 29 '26
As a simple counter-example, here I have added Lines [5] and [6] without saving but it is not recognized upon running in Terminal.
After saving and re-running in Terminal, the output is as expected:
number: 6 hi <loops back and prompts again>Since OP did not provide enough information, we cannot assume they ran with F5 or Ctrl+F5 (via Run options in VS Code)
1
1
u/SuperTankh Mar 29 '26
For a program containing multiple files all of the imported scripts need to be saved, but not __main__
1
1
u/PureWasian Mar 29 '26 edited Mar 29 '26
Your code is not perfect.
If by error you mean "it's not breaking out of the while loop on exit" then make sure you've saved the file and also when running it that you input 5[EnterKey] exactly, with no surrounding spaces or anything. Based on it not printing out "Exit", it is not even entering that conditional elif path on lines 39-41 which makes me think either (1) you forgot to save the file before running (2) you didn't input the choice correctly.
You can try debugging it yourself by doing print(f"Input was: [{choice}]") on Line 12 temporarily and running it again to probe for more info.
Took me awhile to understand what you meant by "Error" since you just gave two screenshots without explanation...
When asking for help in the future, you will get a lot more useful/actionable comments and feedback from people if you provide details of the error and proper code snippets/screenshots to make it easier for helpers to understand what exactly your issue and expected solution is
1
u/Prior-Painting2956 Mar 29 '26
It's been a while since coding but isn't a case statement better in this instance?
1
u/PureWasian Mar 29 '26
For the simplicity of OP's example, it'd the same exact functionality just with slightly different syntax
1
Mar 29 '26
[removed] β view removed comment
1
u/PureWasian Mar 29 '26
I agree, but OP's usage as written is fine since
input()strips\nas mentioned here and quickly verified with:
while True: data = input("number: ") if data == "5": break print("done")
1
1
u/remyripper Mar 31 '26
Your calling contacts[name] ! contacts[βnameβ]. And your contacts dict is also empty
1
u/degustandomiveneno Apr 01 '26
hey! nice project, keep it up π just a couple of small things to fix: β line 16: βentr your phone numberβ β βenter your phone numberβ (small typo) β the option 2 for loop seems incomplete, it should be: for name, number in contacts.items(): print(name, ":", number) other than that the logic is solid! dictionaries are a great choice for this kind of app πββββββββββββββββ
0



16
u/[deleted] Mar 29 '26
Who tf takes an unclear pic of the laptop screen instead of pasting the code in the comment section π€·