gpt4 book ai didi

python - 我如何完成以下检查 list 中的第 2 项和/或完成该计划?

转载 作者:行者123 更新时间:2023-12-01 05:55:05 25 4
gpt4 key购买 nike

这是我正在解决的问题:

Assume that the developers of Myro are developing a new black box function called traveler(String str ) that moves a robot in a specific direction (i.e., North, South, East, or West) for a specific number of feet (e.g., 1 = one foot, 2 = two feet, etc.) The parameter of the traveler( ) function is a String variable that expresses both the direction and the distance. For example, traveler(E2) would move the robot East for two feet while traveler(N6) would move the robot North for six feet.

Write a short segment of code that uses the black box traveler( ) function to move the robot from Point A to Point B in the grid shown below.

  1. Assign the parameters (e.g. N2, etc.) to a data structure.
  2. Traverse the data structure with a loop that supplies the parameters to the traveler( ) function.

Path to follow

The traveler(String str) function does not really exist. Treat it as if it did and simply write a short segment of code to accomplish the task of moving the robot along the pattern given above.

这是我所拥有的:

    def main():
go = [N2, W1, N3, E4, S1, W1, S1, E2]
for g go:
traveler(g)

main()

我的问题是如何完成这件事?

最佳答案

在Python中遍历列表通常是使用 for 来完成的循环

Python 文档中的示例:

>>> # Measure some strings:
... words = ['cat', 'window', 'defenestrate']
>>> for w in words:
... print w, len(w)
...
cat 3
window 6
defenestrate 12

在上面的示例中,len 是一个内置函数。他们将 w 传递给 len 函数。这与您需要使用 traveler 函数执行的操作非常相似。
循环遍历 go 列表中的每个项目。将该元素传递给旅行者

关于python - 我如何完成以下检查 list 中的第 2 项和/或完成该计划?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12991103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com