Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How to make my python script go back to a certain line

–4 votes
asked Mar 31, 2021 by Ryder Johansen (310 points)
So I'm making a python text adventure https://onlinegdb.com/H1HoOW-rO and I'm trying to make an inventory function that you can exit and go back to the other combat choices. how would I make it go back?

2 Answers

+1 vote
answered Mar 31, 2021 by Peter Minarik (79,780 points)
edited Mar 31, 2021 by Peter Minarik

A bit of history: goto

Most languages have a goto instruction to jump into a certain line of code. However, no one ever uses this as this is messy and makes the code unmaintainable (really, if you use goto on a job interview, you will be thrown out immediately, lol).

Also, it seems like Python doesn't even support it at all. :)

There are some (very rare) valid cases when goto can be a valid choice, but I only saw this mostly for C where goto was used to jump to the last few lines of the function and run the cleanup phase before exiting the function, so the cleanup code wouldn't be copy pasted all over the place. Of course, there could have been other ways for this as well (e.g. a structure of if statements).

Structure your code

What you should consider instead is using classes and functions as I've shown you before.

Also, you can make a loop where you keep repeating the things. Consider the following pseudo code:

loop while player is still alive
end loop

So in the above example everything inside the would keep repeating as long as a certain condition is met (in this case, that the player is still alive).

You can consider your own loop for your game needs, but I thin this would work out fine.

And make a class for your inventory, so functionality could be easily reused by a call of a member function.

It's also a good idea to check out various tutorials and/or reference pages about Python so you'd be more familiar with the capabilities of the language. (For instance https://www.w3schools.com/python/default.asp.)

I hope this is not too vague. Give it a try and show what you came up with.

Good luck! :)

commented Mar 31, 2021 by Ryder Johansen (310 points)
so I basically used a while loop to make is so that if inventory was empty and choice was inventory then it would loop back to the choice selection but I got this error:
  File "main.py", line 144
SyntaxError: unexpected EOF while parsing

0 votes
answered Apr 1, 2021 by sourabhskp (140 points)
# Asks which command you want to run.
word = input("Which command? ")

# Runs the command/s.

if word == "info":
    print("Info command.")

elif word == "replace":
    print("Replace command.")

elif word == "ping":
    print("Ping command")

    print("Command not found")
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.