简体   繁体   English

通过Python中的Zapier输入声明变量

[英]Declaring a variable via a Zapier input in Python

I'm trying to do a simple declaration of an inputted variable to an integer, but am receiving an error: 我正在尝试将输入的变量声明为整数,但是却收到错误:

Bargle. gle We hit an error creating a run python. 我们在创建运行python时遇到错误。 :-( Error: Your code had an error! Traceback (most recent call last): File "/tmp/tmpXq4aAP/usercode.py", line 7, in the_function num = int(input['managers']) KeyError: 'num' :-(错误:您的代码有错误!追溯(最近一次调用):文件“ /tmp/tmpXq4aAP/usercode.py”,第7行,在the_function num = int(input ['managers'])KeyError:' num'

The following is the code i'm using: 以下是我正在使用的代码:

num = int(input['num'])

if num >= 100 :
  big_num = true
else:
  big_num = false

return {'big_num': big_num}

Your error is right here: 您的错误就在这里:

num = int(input['num'])

Change those square brackets for round brackets: 将那些方括号更改为圆括号:

num = int(input('num'))

If you are on Python 2 you should use raw_input 如果您使用的是Python 2,则应使用raw_input

num = int(raw_input('num'))

In Zapier, the code: 在Zapier中,代码:

input['varname']

refers to the variable that is passed in the "Code by Zapier" Action. 指在“ Zapier的代码”操作中传递的变量。

The error you are getting sounds to me like you have not defined the num variable prior to your code. 您听到的错误听起来像是您在代码之前未定义num变量。

Also, True and False need to be capitalized. 此外, TrueFalse需要大写。

Otherwise, see below, this setup works... 否则,请参见下文,此设置适用...

在此处输入图片说明

num = int(input['num'])

if num >= 100 :
  big_num = True
else:
  big_num = False

return {'big_num': big_num}

Many of these answers reference the input() built in - we override that in Code by Zapier (since it literally makes zero sense to have user input on an automated script). 这些答案中有许多都引用了内置的input() -我们用Zapier在Code中覆盖了它们(因为在自​​动脚本上进行用户输入在字面上意义为零)。 In it's place is a dictionary defined by some fields above. 它是由上面的某些字段定义的字典。

Definitely confusing for folks unfamiliar with the context of Zapier - we'll look into renaming it and just nulling the input build. 对于不熟悉Zapier上下文的人们来说,这绝对令人困惑-我们将研究重命名它并只是使input构建无效。

输入是一个内核方法,它不能被下标,将语法更改为这样会有语法错误。

num = int(input('num'))

Within Zapier, the proper way to convert Input Data (every input into a Code Step is a String) to Integers is as follows: 在Zapier中,将输入数据(每个输入到代码步都是字符串)转换为整数的正确方法如下:

num = int(input.get('num'))

or 要么

num = int(input['num'])

If the number comes attached with a decimal, strip the unwanted characters from the String before converting to an Integer. 如果数字附带小数点,则在转换为整数之前,请从字符串中删除不需要的字符。 For a number like 80.0 this would look like: 对于80.0这样的数字,它看起来像:

num = int(input['num'][:-2])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM