Random walk generator - looking for optimizations (Python) I just started learning Python and this is the first code I wrote. It's a Random Walk generator. I think I got it to work pretty well but I'm also sure there are more effective and less clunky ways to do this. Could you give me some tips on what to change to make the code shorter/more optimized and just objectively more &quot;pythonian&quot; and better?
# Random Walk in two dimensions

import random

steps = &quot;a&quot;
maximal = &quot;a&quot;
minimal = &quot;a&quot;
Xstep = 0
Ystep = 0
StepSize = 0
PosNeg = 0


def new_line():
    print(&quot; &quot;)


def invalid_enter_number():
    print(&quot;Invalid input! Enter a number.&quot;)
    new_line()


def invalid_enter_positive():
    print(&quot;Invalid input! Enter a positive number.&quot;)
    new_line()


new_line()
print(&quot;Random Walk in two dimensions&quot;)
new_line()

while type(steps) != int:
    try:
        steps = int(input(&quot;How many steps should be done? &quot;))
        while steps &lt;= 0:
            invalid_enter_positive()
            steps = int(input(&quot;How many steps should be done? &quot;))
    except:
        steps = ValueError
        invalid_enter_number()
        continue

while type(maximal) != int:
    try:
        maximal = int(input(&quot;How big is the biggest possible step? &quot;))
        while maximal &lt;= 0:
            invalid_enter_positive()
            maximal = int(input(&quot;How big is the biggest possible step? &quot;))
    except:
        maximal = ValueError
        invalid_enter_number()
        continue

if maximal != 1:
    while type(minimal) != int:
        try:
            minimal = int(input(&quot;How big is the smallest possible step? &quot;))
            while minimal &lt;= 0 or minimal &gt;= maximal:
                if minimal &lt;= 0:
                    invalid_enter_positive()
                    minimal = int(input(&quot;How big is the smallest possible step? &quot;))
                else:
                    print(&quot;Number must be smaller than the biggest possible step!&quot;)
                    new_line()
                    minimal = int(input(&quot;How big is the smallest possible step? &quot;))
        except:
            minimal = ValueError
            invalid_enter_number()
            continue
else:
    minimal = 1

new_line()
print(&quot;Number of steps:&quot;, steps)

if maximal != 1:
    print(&quot;Step size range:&quot;, minimal, &quot;-&quot;, maximal)
else:
    print(&quot;Step size: 1&quot;)

new_line()
print(&quot;Steps:&quot;)

while steps &gt; 0:
    StepSize = random.randint(minimal, maximal)
    PosNeg = random.randint(0, 1)
    chance = random.randint(0, 1)
    if chance == 0 and PosNeg == 0:
        Xstep += StepSize
    elif chance == 0 and PosNeg == 1:
        Xstep -= StepSize
    elif chance == 1 and PosNeg == 0:
        Ystep += StepSize
    else:
        Ystep -= StepSize
    print(&quot;X:&quot;, Xstep, &quot; Y:&quot;, Ystep)
    steps -= 1
Here's one suggestion; other readers will probably offer more.
Several times, you need to get an integer from the user. You
could create a function to handle such details. The code
below shows what the function might look light and how you
would use it.
def int_from_user(prompt):
    while True:
        try:
            n = int(input(prompt + ' '))
            if n &gt; 0:
                return n
            else:
                raise ValueError()
        except ValueError:
            print('Invalid input! Enter a positive number.')

steps = int_from_user('How many steps should be done?')
print(steps)