I've been trying to get this by myself for hours but I'm new to coding, so I don't really know how to fix this. When I call the cost again, it won't add the number I try to add and won't budge off of 0. I want the "cost" integer to add 1 to itself when the user inputs the word "Small".
Asked
Active
Viewed 85 times
0
-
2Please provide a [mre]. – Unmitigated Apr 11 '21 at 03:55
-
Please provide full code as we can not see where the function is called – Yeshwin Verma Apr 11 '21 at 03:59
-
You are missing pranthesis for the function – Yeshwin Verma Apr 11 '21 at 04:00
-
made an edit guys, sorry if it wasn't too clear – washed daniel Apr 11 '21 at 04:04
-
1@washeddaniel, the `cost` inside the method hides the `cost` variable decalred outside. So `cost = cost + 1` only impacts the method parameter `cost` – Gautham M Apr 11 '21 at 04:13
-
return cost from the method and reassign it. To understand this, read whats `pass by reference` & whats `pass by value` – leoOrion Apr 11 '21 at 04:13
-
https://stackoverflow.com/q/40480/7804477 – Gautham M Apr 11 '21 at 04:16
-
@washeddaniel you have mentioned "loop" in the question title, but i don't see any loops in your code. – Gautham M Apr 11 '21 at 04:17
-
@GauthamM and all you guys are the best I fixed it – washed daniel Apr 11 '21 at 04:18
-
Please undo your last edit. The question makes no sense now, and the answer you get also makes no sense because you removed your code. – NomadMaker Apr 15 '21 at 22:39
1 Answers
1
It looks like you're passing the cost to the function as a parameter, which for primitives (likeint
or float
) means that what you're working with inside the function is not the cost
variable declared outside the function, but merely a copy with the same value. So when you increment the variable, you're actually incrementing the copy that was passed into the function.
The fix should be quite simple: just remove the cost
parameter and you'll automatically be working with the global variable instead.
Also, it seems weird that you're calling a function (presumably) directly inside the class body which should normally give a compiler error.

linux_user36
- 113
- 1
- 8