Simple console calculator in python I am new to python, I made a calculator but I need to make it calculating unlimited numbers and to shorten the code more, how can I improve it?
Any help is appreciated.
while(True):
    print()
    try:
        nums = int(input(&quot;How many numbers you want to calculate?\n&quot;))
    # x = float(input(&quot;Enter the first number: &quot;))
    # y = float(input(&quot;Enter the second number: &quot;))
    # z = float(input(&quot;Enter the third number: &quot;))
    # w = float(input(&quot;Enter the forth number: &quot;))
    # v = float(input(&quot;Enter the fifth number: &quot;))

        if nums == 2:
            x = float(input(&quot;Enter the first number: &quot;))
            y = float(input(&quot;Enter the second number: &quot;))
            print()
            func = int(input('''What do you want to do?
            1 to add
            2 to subtract
            3 to multiply
            4 to divide
            '''))
            if func == 1:
                print(x+y)
            if func == 2:
                print(x-y)
            if func == 3:
                print(x*y)
            if func == 4:
                print(x/y)
        if nums == 3:
            x = float(input(&quot;Enter the first number: &quot;))
            y = float(input(&quot;Enter the second number: &quot;))
            z = float(input(&quot;Enter the third number: &quot;))
            print()
            func = int(input('''What do you want to do?
            1 to add
            2 to subtract
            3 to multiply
            4 to divide
            '''))
            if func == 1:
                print(x+y+z)
            if func == 2:
                print(x-y-z)
            if func == 3:
                print(x*y*z)
            if func == 4:
                print(x/y/z)
        if nums == 4:
            x = float(input(&quot;Enter the first number: &quot;))
            y = float(input(&quot;Enter the second number: &quot;))
            z = float(input(&quot;Enter the third number: &quot;))
            w = float(input(&quot;Enter the forth number: &quot;))
            print()
            func = int(input('''What do you want to do?
            1 to add
            2 to subtract
            3 to multiply
            4 to divide
            '''))
            if func == 1:
                print(x+y+z+w)
            if func == 2:
                print(x-y-z-w)
            if func == 3:
                print(x*y*z*w)
            if func == 4:
                print(x/y/z/w)
        if nums == 5:
            x = float(input(&quot;Enter the first number: &quot;))
            y = float(input(&quot;Enter the second number: &quot;))
            z = float(input(&quot;Enter the third number: &quot;))
            w = float(input(&quot;Enter the forth number: &quot;))
            v = float(input(&quot;Enter the fifth number: &quot;))
            print()
            func = int(input('''What do you want to do?
            1 to add
            2 to subtract
            3 to multiply
            4 to divide
            '''))
            if func == 1:
                print(x+y+z+w+v)
            if func == 2:
                print(x-y-z-w-v)
            if func == 3:
                print(x*y*z*w*v)
            if func == 4:
                print(x/y/z/w/v)
        if nums &gt; 5:
            print(&quot;The calculator handles 5 numbers maximum!&quot;)
    except(ZeroDivisionError):
        print(&quot;You can never divide by Zero!&quot;)
    except(ValueError):
        print(&quot;Check your input.&quot;)
Loops would be the perfect tool here to reduce duplication; although getting it exactly as you have will be difficult due to you currently spelling out numbers (&quot;first&quot;, &quot;second&quot;, &quot;third&quot;...). For the sake of simplicity, I'm going to ignore the numeric words, because producing those is non-trivial and a whole project on its own unless you use an existing library.
In their place, I'm going to use a simple function. This can be optimized using a dictionary acting as a case, but I think it's fine as-is besides some minor duplication:
def format_number(n: int) -&gt; str:
    last_digit = str(n)[-1]
    if last_digit == &quot;1&quot;:
        return f&quot;{n}st&quot;
    elif last_digit == &quot;2&quot;:
        return f&quot;{n}nd&quot;
    elif last_digit == &quot;3&quot;:
        return f&quot;{n}rd&quot;
    else:
        return f&quot;{n}th&quot;

&gt;&gt;&gt; format_number(2)
'2nd'
&gt;&gt;&gt; format_number(5)
'5th'
&gt;&gt;&gt; format_number(1)
'1st'

It goes a little bit wonky in the teens (&quot;12nd&quot;), but like I said, it isn't a super straightforward problem, and I don't want to sidetrack the review.

First, you can ask for numbers using a loop, and in the loop, place the entered numbers into a list:
nums = int(input(&quot;How many numbers you want to calculate?\n&quot;)) 

entered_nums = []
for n in range(nums):
    x = float(input(f&quot;Enter the {format_number(n + 1)} number: &quot;))
    entered_nums.append(x)

When run, I get:
How many numbers you want to calculate?
3
Enter the 1st number: 9
Enter the 2nd number: 8
Enter the 3rd number: 7

And entered_nums now holds [9, 8, 7].
If you're at all familiar with list comprehensions though, you'll notice that that loop can be simplified a bit:
entered_nums = [float(input(f&quot;Enter the {format_number(n + 1)} number: &quot;))
                for n in range(nums)]

And this will have the same effect with a bit less bulk.

Once you have the numbers to do math on, you need to ask for the operation to apply, then apply that operation.
For the first, I'm going to import the operator module because it'll make life much easier. operator.add for example is the same thing as +; only it can be stored as an object.
I'm going to use a dictionary to store the menu code to operator relationship:
from operator import add, sub, mul, truediv
. . .

func = int(input('''What do you want to do?
            1 to add
            2 to subtract
            3 to multiply
            4 to divide
            '''))

code_to_op = {1: add,
              2: sub,
              3: mul,
              4: truediv}

This allows for easily doing math:
&gt;&gt;&gt; code_to_op = {1: add,
                  2: sub,
                  3: mul,
                  4: truediv}

&gt;&gt;&gt; op = code_to_op[3]
&gt;&gt;&gt; op(3, 5)
15

Note though that a bad dictionary lookup will cause an error. code_to_op[5] for example will raise a KeyError. I didn't handle that for simplicity and because you currently aren't handling bad input. It should be dealt with though.

So now once we can get the operator that the user wants, we need to apply it to the numbers they entered. Again, a loop (or something that acts as a loop) is our friend here. We'll need to keep track of the current result of the equation (the &quot;accumulator&quot;), and a loop to do the math:
acc = entered_nums[0]  # The first number they entered will be our starting accumulator
for entered_num in entered_nums[1:]:  # And we'll loop over all the numbers after the first
    acc = op(acc, entered_num)

print(acc)

If the user entered in the numbers 2, 3, and 4, and then entered in 1 for the operator (add), acc would hold 9.0 at the end of that loop.
The acc+loop pattern is incredibly common in programming though. So common in fact that it has its own name: reduce:
from functools import reduce

. . .

result = reduce(op, entered_nums)

reduce here automates the looping for us. It's essentially equivalent to the previous loop that calculated the same value.

After taking that all into consideration, I'm left with:
from operator import add, sub, mul, truediv
from functools import reduce

def format_number(n: int) -&gt; str:
    if n == 1:
        return f&quot;{n}st&quot;
    elif n == 2:
        return f&quot;{n}nd&quot;
    elif n == 3:
        return f&quot;{n}rd&quot;
    else:
        return f&quot;{n}th&quot;

while True:
    try:
        nums = int(input(&quot;How many numbers you want to calculate?\n&quot;))

        entered_nums = [float(input(f&quot;Enter the {format_number(n + 1)} number: &quot;))
                        for n in range(nums)]

        code_to_op = {1: add,
                      2: sub,
                      3: mul,
                      4: truediv}

        func = int(input('''What do you want to do?
                    1 to add
                    2 to subtract
                    3 to multiply
                    4 to divide
                    '''))

        op = code_to_op[func]

        acc = entered_nums[0]  # The first number they entered will be our starting accumulator

        for entered_num in entered_nums[1:]:  # And we'll loop over all the numbers after the first
            acc = op(acc, entered_num)

        print(acc, &quot;\n&quot;)

    except(ZeroDivisionError):
        print(&quot;You can never divide by Zero!&quot;)

    except(ValueError):
        print(&quot;Check your input.&quot;)

And an example run:
How many numbers you want to calculate?
6
Enter the 1st number: &gt;? 9
Enter the 2nd number: &gt;? 8
Enter the 3rd number: &gt;? 7
Enter the 4th number: &gt;? 6
Enter the 5th number: &gt;? 5
Enter the 6th number: &gt;? 4
What do you want to do?
                    1 to add
                    2 to subtract
                    3 to multiply
                    4 to divide
                    &gt;? 1
39.0

There is still a lot that can be mentioned (grouping things into functions, fixing some error handling), but I need to start studying for an exam :)