Python Forum
I need help with my code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I need help with my code
#1
Hi there everyone
I am writing a fake medical database for a assignment and there is an unexpected uninedent in my script, would someone help me correct this please, Thank you Heart
def mainMenu():
        print("1.John Smith")
        print("2.Lucy Jones")
        print("3.Edward Johnson")
        print("4.Amy Harris")
        print("5.Christopher McDonald")
        print("6.Naomi Scott")
        print("7.Stewart Mackay")
while True: 
        try:
         selection=int(input("Enter Patient Number"))
         if selection==1:
                        JS()
         if selection==2:
                        LJ()
         if selection==3:
                        EJ()
         if selection==4:
                        AH()
         if selection==5:
                        MC()
         if selection==6:
                        NS()
         if selection==7:
                        SM()
                    
else:
        print("No Patient Found")
mainMenu():
        except ValueError:
                print("Invalid Patient Name")
def JS():
        print("DoB 03/01/1952 Male Diabetes T2")
        anykey=input("Enter Any Key To Return")
def LJ():
        print("DoB 28/06/1984 Female HIV")
        anykey=input("Enter Any Key To Return")
def EJ():
        print("DoB 21/04/2001 Male Lupus")
        anykey=input("Enter Any Key To Return")
def AH():
        print("DoB 01/09/1944 Female Diabetes T2")
        anykey=input("Enter Any Key To Return")
def MC():
        print("DoB 20/12/1975 Male Epilepsy")
        anykey=input("Enter Any Key to Return")
def NS():
        print("DoB 17/08/1997 Female Crohn's Disease")
        anykey=input("Enter Any Key To Return")
def SM():
        print("DoB 12/05/1964 Male Heart Disease")
        anykey=input("Enter Any Key To Return")
''
Reply
#2
The traceback will always show you where to look for error. In this case even without traceback is clear that indentation of lines 11-25 is mixed/incorrect.
As an advice - use 4 spaces per level, not 8
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
It describes mainMenu(): as being invalid syntax. Why is this?
def mainMenu():
    print("1.John Smith")
    print("2.Lucy Jones")
    print("3.Edward Johnson")
    print("4.Amy Harris")
    print("5.Christopher McDonald")
    print("6.Naomi Scott")
    print("7.Stewart Mackay")
while True:
    try:
        selection=int(input("Enter Patient Number"))
        if selection==1:
            JS()
            if selection==2:
                LJ()
                if selection==3:
                    EJ()
                    if selection==4:
                        AH()
                        if selection==5:
                            MC()
                            if selection==6:
                                NS()
                                if selection==7:
                                    SM()
                    
        else:
            print("No Patient Found")
            mainMenu():
                except: ValueError:
                    print("Invalid Patient Name")
                    def JS():
                        print("DoB 03/01/1952 Male Diabetes T2")
                        anykey=input("Enter Any Key To Return")
                        def LJ():
                            print("DoB 28/06/1984 Female HIV")
                            anykey=input("Enter Any Key To Return")
                            def EJ():
                                print("DoB 21/04/2001 Male Lupus")
                                anykey=input("Enter Any Key To Return")
                                def AH():
                                    print("DoB 01/09/1944 Female Diabetes T2")
                                    anykey=input("Enter Any Key To Return")
                                    def MC():
                                        print("DoB 20/12/1975 Male Epilepsy")
                                        anykey=input("Enter Any Key to Return")
                                        def NS():
                                            print("DoB 17/08/1997 Female Crohn's Disease")
                                            anykey=input("Enter Any Key To Return")
                                            def SM():
                                                print("DoB 12/05/1964 Male Heart Disease")
                                                anykey=input("Enter Any Key To Return")
Reply
#4
you are really doing strange stuff here.

1. you need to define functions before you can call them. (i.e. lines starting from 32 should be before you try to call them (i.e. before current line 9)
2. No indentation is needed - I don't know why you indent each of these functions further and further
3. line 29 mainMenu(): - no need od :
4. line 30 - except should be at same level as try
5. maybe the biggest problem with your code is repetition. This is indicative you need to refactor your code. Hint: - you can have the patient name as key and their birthday as value in a dictionary. This assume no repeating names. Or you can find more suitable data structures like namedtuple to hold patient information and access it from single function.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

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