-1

I have a project to write code in Python that will control a robot's movements in a 10 by 10 grid. First i would like to point out that i am a beginner so it would be better for me if i can get simple lines of code that i can digest.

So, the project asks for:

A 10 by 10 grid, with the robot starting from the uppermost left position which is X(0,0).

Moving from X(0,0) down one tile will increase the value to (1,0) until (9,0) which is the downmost left corner, while moving from X(0,0) to the right will increase each time by (0,1) until (0,9) upper right corner. Moving from position (9,0) to the right will again be up to (9,9).

It will accept commands to move Up, down, left, right (u ,d ,l, r). Each command should be given together with and integer number that denotes the steps to the given direction (for ex. u5, or d2).

The user will give commands continuously until ENTER is pressed which will make the program exit.

After each command, the program must calculate the position of the robot and print out a message with it.

In the case that the user gives a command that cannot be executed or will make the robot go outside the grid, then an error message must appear. It must also give the error message in instances where X or Y => N.

Thanks to anyone who will take the time to help me!

I have not tried anything yet as i am in a loss of what to do and how.

gallahant
  • 1
  • 2

1 Answers1

0
while True:
try:
    n = int(input("Δώσε διαστάσεις του grid:"))
    n1 = n*n
    print(f"Το ρομπότ κινείται σε χώρο {n1}. Η αρχική του θέση είναι:", x, ",", y)
except ValueError:
    print("Παρακαλώ δώσε θετικό ακέραιο αριθμό")
    continue

while True:
    try:
        move = input("Δώσε κίνηση:")
        if move[0] == 'r':
            if int(move[1:])+y >= n1:
                print("ΣΦΑΛΜΑ! Κίνηση έξω από τα όρια του χώρου.\nΗ θέση του ρομπότ παραμένει:", x, ",", y)
            else:
                y = y+int(move[1:])
                print("Η νέα θέση του ρομπότ είναι:", x, ",", y)

        if move[0] == 'l':
            if int(move[1:])-y > 0:
                print("ΣΦΑΛΜΑ! Κίνηση έξω από τα όρια του χώρου.\nΗ θέση του ρομπότ παραμένει:", x, ",", y)
            else:
                y = y-int(move[1:])
                print("Η νέα θέση του ρομπότ είναι:", x, ",", y)

        if move[0] == 'u':
            if int(move[1:])-x > 0:
                print("ΣΦΑΛΜΑ! Κίνηση έξω από τα όρια του χώρου.\nΗ θέση του ρομπότ παραμένει:", x, ",", y)
            else:
                x = x-int(move[1:])
                print("Η νέα θέση του ρομπότ είναι:", x, ",", y)

        if move[0] == 'd':
            if int(move[1:])+x >= n1:
                print("ΣΦΑΛΜΑ! Κίνηση έξω από τα όρια του χώρου.\nΗ θέση του ρομπότ παραμένει:", x, ",", y)
            else:
                x = x+int(move[1:])
                print("Η νέα θέση του ρομπότ είναι:", x, ",", y)
    except ValueError:
        print("ΣΦΑΛΜΑ! Κίνηση έξω από τα όρια του χώρου.\nΗ θέση του ρομπότ παραμένει:", x, ",", y)
        continue
while move == "":
    print("Τερματισμός προγράμματος")
    exit()
gallahant
  • 1
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 10 '22 at 21:42