r/learnpython • • 2h ago

Learning programming

I’m learning programming for college for a cybersecurity degree. I need to make my own program that works(in Python). I have my different options i .e. lists all laid out. I wrote a flowchart and pseudo (both required for the project) as I’m coding everything one of the lists is getting skipped entirely if the user enters yes instead of no. And if the user enters no it gives them the code of them dying but instead of stopping the loop entirely it reloops the code again and still skips one of the lists. I’m not quite sure the best way to fix this. The lists are all the same length, in brackets and spelled out properly. Any suggestions or help would be appreciated. Updated: (After I posted I realized I gave zero code. Also I have messed with the bottom half of the code a decent amount.)

def printItem(targetlist, index):
    print(targetlist[index])


def main():
    loc = ["Attic", "Hallway", "Living room", "Front door"]
    dsrp = [
        "Dark, quiet with an eerie feeling.",
        "Flickering lights showing a bloody rug.",
        "Static on the TV with paper strewn across the floor off the table.",
        "Unlocked door with a breeze coming through the cracked glass in the pane."
    ]
    nos = [
        "You hear a faint shuffle of footsteps.",
        "There is a scurrying sound of something...",
        "Suddenly your name is being whispered from the shadows.",
        "A whoosh brings..."
    ]
    enmy = [
        "It's a raccoon!",
        "A rat ran across the floor!",
        "The killer is right behind you!",
        "A shadow jumps out!"
    ]
    print("You wake up and realize there is a killer somewhere in the house with you.")
    print("ESCAPE!")


    for i in range(len(loc)):
        current_loc=loc[i]
        current_dsrp=dsrp[i]
        current_nos=nos[i]
        current_enmy=enmy[i]
        print("\nYou find yourself in,:" + current_loc + ".")
        print(current_dsrp)
        print(current_nos)


        choice = input("\nDo you want to investigate? (Yes or No): ").lower()
        while choice != "yes" and choice != "no":
                print("You cower away and die of fear!")
                
        else:
            choice == "yes"
            printItem(current_enmy)
            conChoice = input("\nDo you wish to continue? (Yes or No): ").lower()
                    
            while conChoice != "yes" and conChoice != "no":
                         print("You curl in a ball and cry.")
                     
            if conChoice == "no":
                 conChoice == "no"
                 print("You have died from dehydration and fear!")
            else:
                print("You have escaped the killer!")


main()def printItem(targetlist, index):
    print(targetlist[index])


def main():
    loc = ["Attic", "Hallway", "Living room", "Front door"]
    dsrp = [
        "Dark, quiet with an eerie feeling.",
        "Flickering lights showing a bloody rug.",
        "Static on the TV with paper strewn across the floor off the table.",
        "Unlocked door with a breeze coming through the cracked glass in the pane."
    ]
    nos = [
        "You hear a faint shuffle of footsteps.",
        "There is a scurrying sound of something...",
        "Suddenly your name is being whispered from the shadows.",
        "A whoosh brings..."
    ]
    enmy = [
        "It's a raccoon!",
        "A rat ran across the floor!",
        "The killer is right behind you!",
        "A shadow jumps out!"
    ]
    print("You wake up and realize there is a killer somewhere in the house with you.")
    print("ESCAPE!")


    for i in range(len(loc)):
        current_loc=loc[i]
        current_dsrp=dsrp[i]
        current_nos=nos[i]
        current_enmy=enmy[i]
        print("\nYou find yourself in,:" + current_loc + ".")
        print(current_dsrp)
        print(current_nos)


        choice = input("\nDo you want to investigate? (Yes or No): ").lower()
        while choice != "yes" and choice != "no":
                print("You cower away and die of fear!")
                
        else:
            choice == "yes"
            printItem(current_enmy)
            conChoice = input("\nDo you wish to continue? (Yes or No): ").lower()
                    
            while conChoice != "yes" and conChoice != "no":
                         print("You curl in a ball and cry.")
                     
            if conChoice == "no":
                 conChoice == "no"
                 print("You have died from dehydration and fear!")
            else:
                print("You have escaped the killer!")


main()
2 Upvotes

5 comments sorted by

View all comments

0

u/p186 2h ago

Not knowing the code, make sure you have a way of ending a loop and then that decision the user makes should update that condition to trigger the loop to be invalid/stop. You probably already know this.

I need some caffeine, so I’m not able to assess the issue without the code. Not sure how many files the program has but if you have it in a public git repo, link to that or paste the relevant file code into pastebin & share the link. That way we can look at it.