Python#

VEX IQ(第二代)Python API 参考文档解释了每个命令、方法和函数的作用、输入、返回值以及如何在项目中使用它们。

当您想在将命令添加到项目之前了解该命令、检查它接受或返回的值,或者比较类别中的相关命令时,请使用此参考。

VEXcode IQ(第二代)Python 项目使用 Python 命令。而 Blocks 和 C++ 项目则使用其他 API 模块。

注意: IQ(第二代)大脑通过内存有限的虚拟机运行 Python 程序。虽然源代码大小有限制,但程序能否运行还取决于其复杂度(例如函数、变量和逻辑的数量)。因此,即使程序大小不超过限制,大型或复杂的程序也可能无法编译或运行。C/C++ 程序通常受 IQ(第二代)的这些限制影响较小。

如何阅读方法条目#

大多数 Python 条目都包含以下部分:

  • 命令名称 - 命令、方法或函数的正式名称。

  • 描述 - 解释该命令的作用以及何时使用该命令。

  • 用法 - 显示用于在代码中编写命令的语法。

  • 参数 - 列出命令接受的输入,并解释每个输入的作用。

  • 返回值 - 说明命令返回的内容(如果有返回值)。

  • 示例代码 - 展示了在项目中使用该命令的一种方法。

常用 Python API 元素#

元素

它的含义

方法

A command called on an object such as drivetrain.drive(…).

功能

A command called directly, such as wait(…) or print(…).

范围

传递给方法或函数的值,用于控制其执行的操作。

可选参数

可以省略的参数,以便使用默认行为。

返回值

命令返回的值,例如数字、文本或布尔值。

Python基础词汇#

学期

它的含义

多变的

A name that stores a value, such as speed = 50.

细绳

Text inside quotes, such as “Hello”.

整数

A positive or negative number, such as 90.

漂浮

A number with a decimal, such as 25.5.

布尔值

A value that is either True or False.

争论

A value passed into a method or function call, such as REVERSE in drivetrain.drive(REVERSE, 20, PERCENT).

示例方法条目#

驾驶#

drive moves the robot forward or reverse forever. The robot will continue to move until it is given another action, like turning or stopping.

Usage:
drivetrain.drive(direction, velocity, units)

参数

描述

direction

The direction the robot moves: FORWARD or REVERSE.

velocity

Optional. The velocity to drive with from 0% to 100% when using PERCENT, from 0 to 120 rpm when using RPM, or from 0 to 720 degrees per second when using VelocityUnits.DPS. This can be an integer or decimal (float). If no velocity is provided, the robot drives at the current drive velocity.

units

Optional. The velocity unit: PERCENT (default), RPM (rotations per minute), or VelocityUnits.DPS (degrees per second).

# Drive forward then stop
drivetrain.drive(FORWARD)
wait(2, SECONDS)
drivetrain.stop()

# Drive slowly in reverse then stop
drivetrain.drive(REVERSE, 20, PERCENT)
wait(2, SECONDS)
drivetrain.stop()