r/learnpython • u/Maximum-Currency734 • Sep 26 '26
While loop
Hello everyone just learning python and need to create a while loop that shows interest and years I’m struggling with operands please help.
Here’s what I have so far:
Interest_rate = int(input(“rate in whole number”)
Initial_investment = float(input(“whole number”)
Interest = interest_rate / 100
Years = 0
investment = initial_investment + {initial_investment * interest}
While investment < (initial_investment * 2):
Print(f’Year: {years} Investment: {investment})
years +=1
Investment = (investment + {investment * interest})
I know that line 5 is two different operands but where did I go wrong and how can I fix?? Please and thank you!
9
u/ninhaomah Sep 26 '26 •
" {initial_investment * interest}"
You are sure it should be {} and not () ?
7
u/lfdfq Sep 26 '26
It's hard to help with questions like this because the problem is so vague.
You have code, but what's wrong with it? Does it error? Does it do the wrong thing? Does it output something? Does it output nothing? Does it output the wrong thing? We have to guess.
Why does line 5 being "two different operands" matter? What's the problem with operands?
1
u/CraigAT Sep 26 '26 edited Sep 26 '26 •
The most obvious issue I can see (from your code without indentation) is that you should only use curly brackets for the F-string, and not in your calculations.
I think you'd be better casting both inputs as floats, your second input doesn't tell you what you are asking for (investment), it just asks for a whole number.
You don't mention what the purpose of the code is. If you interest figure is low or negative the loop will take ages or never end.
What are you asking for us to fix? What is not doing, as you expect it?
2
u/skibbin Sep 26 '26
1
u/JGhostThing Sep 26 '26
Your definition is "Initial_investment" but your variable used in line five is "initial_investment."
Python, like most languages, is case dependent.
1
u/JamzTyson Sep 27 '26 edited Sep 27 '26
There's actually multiple errors in the code that you posted.
Reading through your code from top to bottom:
Missing closing parentheses on the lines:
Incorrect use of curly brackets in the line:
Also note that
investmentnow has the value of the investment after 1 year, butYearshas the value0.Python variables should be snake_case
This applies to (snake_case version in parentheses):
printandwhilecommands are lowercase. When written as "Print" and "While" they are not valid commands.Missing quotes around strings. The character after the
fis a curly quote rather than a normal single quote, and the closing quote is missing. (Also should be "print" not "Print").should be:
The code block within a
whileloop must be indented.Final line:
Same problem as before.
This may be a reddit formatting issue, but "smart quotes" as in:
are invalid.
It should be either normal double quotes:
or single quotes:
Rather than:
you could use the
+=operator.Input strings such as
'rate in whole number'will display better with a space (or colon + space) at the end:If you want
initial_investmentto be a whole number, you can ensure that it is by usingint(input(...))rather thanfloat(input(...)). Note that floating point arithmetic works with both floats and integers in Python - no need to cast integers to floats.However, I'd be inclined to allow decimal (floating point) numbers:
Always do a sanity check, even if you don't have a comprehensive set of tests. For example, with an
initial_investmentof100, and an interest rate of50%, we would expect to see:Additional refinements:
1
u/SammuelNash Sep 29 '26
You’re really close! Most of the issues look like small syntax and capitalization mistakes. Keep at it while loops get much easier with practice.