r/learnpython • u/MadHedge • 2h ago
Fixing my skipped list
So I just posted a post and had to delete because I realized I fucked up and didn’t upload any code with it. I have a project in Python that I have to make. Mine is going to be a text adventure game and it keeps skipping the enmy list. It also just reloops regardless of if the user dies or not.
Here is the section of the code:
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()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
u/atarivcs 2h ago
When you say "it also just reloops", which loop do you mean?
The for i in range(len(loc)) loop?
The while choice != "yes" and choice != "no": loop?
The while conChoice != "yes" and conChoice != "no": loop?
It might help if you posted some sample output so we can see exactly what the code is doing.
1
u/MadHedge 1h ago
When I run it. And I enter yes or no in the terminal to check to see if it’s running properly. It skips the list enmy and runs back up to the new loc/dsrp/nos even if it’s supposed to stop the program.
2
u/atarivcs 1h ago
So you mean the
for i in range(len(loc))loop at the top of main() keeps running?You do not have a
breakstatement to exit the loop, so yeah of course it executes again.How did you expect that loop to finish?
1
u/MadHedge 1h ago
Yes that’s what I mean. Thank you! I’m still trying to get the hang of this just a bit of a struggle on some of it.
2
u/atarivcs 1h ago
If you want a loop to finish early, you need to put a
breakstatement at the appropriate place.Or, since this is inside a function, you could also use
return.
3
u/atarivcs 2h ago
Please edit your post to correct the formatting.
Indentation is very important in python, and this code has none. We can't tell where the loops and if/then statements are supposed to start and end.