Validating initial inputs then a joint validation test The code works as intended but I believe it is very inefficient. The calculation of weights is simple, but I imagine this would be silly, for example, a 100 asset portfolio. The first problem is that my code returns to the beginning of the loop if the weight for y is entered incorrectly. Help with this would be amazing.
Secondly, as alluded to above, this is an inefficient approach. Someone entering 100 weights would not repeat this process. Any ideas where I could find some help for this approach would be great.
r_x=0.15
r_y=0.21
while True:
    try: w_x=float(input(&quot;Enter the weight of stock x in your portfolio: &quot;))
    except ValueError:
        print(&quot;Please enter the weight using numbers&quot;)
        continue
    else:
        if w_x&lt;0 or w_x&gt;1: #seperating it instead of doing it altogether corrected this error for me
            print(&quot;Please enter the number as a decimal. Remember, your maximum investment is 100%.&quot;)
            continue
    try: w_y=float(input(&quot;Enter the weight of stock y in your portoflio: &quot;))
    except ValueError:
        print(&quot;Please enter the weight using numbers&quot;)
    else:
        if w_y&lt;0 or w_y&gt;1:
            print(&quot;Please enter the numbers as a decimal. Remember, your maximum investment is 100%.&quot;)
            continue
        else:
            if w_x+w_y&lt;0 or w_x+w_y&gt;1:
                print(&quot;Remember, the total weight of the portfolio is 100%. The weights should add to 1.&quot;)
                continue
            else:
                r_p=r_x*w_x+r_y*w_y
                r_p=r_p*100
                print(&quot;The total return on your portoflio is %.2f&quot;%r_p +&quot;%&quot;)
                break
You should write a function that takes care of asking the user for input. It should be able to assign a type and validate the input. This way you can continue asking for a valid input for each of the variables independently. It also allows you to validate the second variable using the value of the first.
def ask_user(message, type_=str, valid=lambda x: True, wrong_type=&quot;Wrong type!&quot;, invalid=&quot;Invalid!&quot;):
    while True:
        try:
            x = type_(input(message))
        except (TypeError, ValueError):
            print(wrong_type)
            continue
        if valid(x):
            return x
        else:
            print(invalid)

Which you can use like this:
if __name__ == &quot;__main__&quot;:
    r_x = 0.15
    r_y = 0.21
    w_x = ask_user(&quot;Enter the weight of stock x in your portfolio: &quot;,
                   type_=float,
                   wrong_type=&quot;Please enter the weight using numbers&quot;,
                   valid=lambda w_x: 0 &lt;= w_x &lt;= 1,
                   invalid=&quot;Please enter the number as a decimal. Remember, your maximum investment is 100%.&quot;)
    w_y = ask_user(&quot;Enter the weight of stock y in your portoflio: &quot;,
                   type_=float,
                   wrong_type=&quot;Please enter the weight using numbers&quot;,
                   valid=lambda w_y: 0 &lt;= w_x + w_y &lt;= 1,
                   invalid=&quot;Remember, the total weight of the portfolio is 100%. The weights should add to 1.&quot;)
    r_p = r_x * w_x + r_y * w_y
    print(f&quot;The total return on your portoflio is {r_p:.2%}&quot;)

I also used the newer f-strings for string formatting, with the % format code, which takes care of that multiplication by one hundred. I also added a if __name__ == &quot;__main__&quot;: guard to allow importing from this script without the code being run.
Python has an official style-guide, PEP8. It recommends using spaces around = when using it for assignment, but not when using it for keyword arguments. Similarly, operators like + and * should generally also be surrounded by spaces.

If you want to be able to input n weights, I would add another function:
def ask_n_weights(message, n, type_=str, wrong_type=&quot;Wrong type!&quot;, invalid=&quot;Invalid!&quot;):
    weights = []
    for i in range(1, n + 1):
        weights.append(ask_user(message.format(i),
                                type_=type_,
                                wrong_type=wrong_type,
                                valid=lambda w: 0 &lt;= w + sum(weights) &lt;= 1,
                                invalid=invalid))
    return weights

This uses the fact that sum([]) = 0 to also be correct for the first variable. In order not to loose the ability to know which weight you are entering I made it so that message should have one placeholder:
ask_n_weights(&quot;Enter weight {}: &quot;, 4, type_=float)
# Enter weight 1: 0.25
# Enter weight 2: 0.25
# Enter weight 3: 0.25
# Enter weight 4: 0.25
# [0.25, 0.25, 0.25, 0.25]

You could also take an iterable of variable names instead of n.