You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pharo Smalltalk中循环无法更新变量值的问题排查求助

Troubleshooting Why puntaje and div Stay at 0.0 in Your Smalltalk Loop

Hey there! Let's break down why your loop isn't updating those variables, even when you know your dictionaries have data. Based on Smalltalk's behavior and your code snippet, here are the most probable causes:

1. You're accidentally redefining variables inside the do: block

Smalltalk uses lexical scoping, which means if you declare puntaje or div again inside the block (with a vertical bar | declaration), you'll be modifying a local block variable instead of the outer one you initialized. For example, if your code looks like this:

auxiliarPelis do: [:each | | puntaje div |
    puntaje := puntaje + (puntajePeliculas at: each).
    div := div + 1]

The puntaje and div here are brand new variables, completely separate from the ones you set to 0.0 earlier. Fix this by removing any duplicate variable declarations inside the block.

2. Your truncated code is missing the actual update logic

Your snippet cuts off at puntaj...—if you didn't finish writing the lines that modify puntaje and div, the loop is just iterating without changing anything. You need explicit assignment statements like:

auxiliarPelis do: [:each |
    puntaje := puntaje + (puntajePeliculas at: each). "Add the movie's score"
    div := div + 1. "Increment the count for averaging"]

Without these lines, puntaje and div will stay stuck at their initial 0.0 values.

3. auxiliarPelis is actually empty

You confirmed your dictionaries have data, but the listaPeliculasPorDirector: director method might be returning an empty collection. This could happen if the director name doesn't match exactly with keys in your dictionaries, or if the method has a logic error. To test this, add a debug line right after getting auxiliarPelis:

auxiliarPelis := aux listaPeliculasPorDirector: director.
Transcript show: 'Number of movies for director: ', auxiliarPelis size asString; cr.

If the output is 0, the loop never runs, so your variables never get updated.

4. Less likely: You're using invalid operations for Float variables

Smalltalk numbers (like Float) are immutable, but that's okay—you just assign the result of the calculation back to the variable. However, if you tried using a method that doesn't exist for Floats (like puntaje add: ... instead of puntaje := puntaje + ...), your code would throw an error. If you're not seeing errors, this is probably not the issue, but it's worth double-checking your syntax.

Start with the simplest checks first: verify auxiliarPelis has elements, then confirm your loop includes the update lines, and make sure you're not redefining variables inside the block. That should resolve the problem!

内容的提问来源于stack exchange,提问作者Martin Curzel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:44:11