简体   繁体   English

为变量分配两个值

[英]Assigning two values to a variable

I will try and explain this as well as possible without giving too many details on what I'm trying to do. 我将尽力而为地解释这一点,而不会给出我要做什么的太多细节。

I have a website Website1 with a product x that has a value of 1 I have another website Website2 with the same product x that has a value of 2. 我有一个网站Website1 ,其产品x的值为1我有另一个网站Website2 ,其产品x的值为2。

The product "x" has two values that need to be paired. 乘积“ x”具有两个需要配对的值。 Does this happen using a tuple? 使用元组会发生这种情况吗?

x = [1, 2]

On Website2 I need to check a list of line items one by one in a loop. Website2我需要循环检查一个订单项列表。 When it finds product x and it matches the joined value of 2 from Website1 in the tuple, I need to then perform a function to change the fulfillment value, the move onto the next line item to perform the next check. 当它找到产品x并与元组中Website1的连接值2相匹配时,我需要执行一个函数来更改实现值,然后移至下一个订单项以执行下一个检查。 Here's what I have so far and I'm not even certain if the logic is anywhere close. 这是我到目前为止的内容,我甚至不确定逻辑是否接近。

x = [1, 2]

if Website1 == x:
LineItem.find_element_by_xpath('./td[4]/div/div/input').clear()
time.sleep(2)
LineItem.find_element_by_xpath('./td[4]/div/div/input').send_keys("1")

else:
LineItem.find_element_by_xpath('./td[4]/div/div/input').clear()
time.sleep(2)
LineItem.find_element_by_xpath('./td[4]/div/div/input').send_keys("0")

Yes, you can easily use tuples to assign two values to a variable (the entire tuple is being assigned to the variable): 是的,您可以轻松地使用元组为变量分配两个值(整个元组都被分配给变量):

x = (val1, val2)
x[0] == val1  # True
x[1] == val2  # True

Side note: [] defines a list, not a tuple. 旁注: []定义列表,而不是元组。 The latter is the immutable variant of the former 后者是前者的不变形式

You should use tuples. 您应该使用元组。

x = (1, 2)

To get the values from tuples you simply do: 要从元组获取值,您只需执行以下操作:

x[0] to get the first value, x[1] for the next and so on. x[0]获取第一个值, x[1]获取下一个值,依此类推。

myValues = (a, b, c)

This will create a tuple, in which you can store multiple values. 这将创建一个元组,您可以在其中存储多个值。 To get the different values, just use myValues[0] to get the first element, for example. 要获取不同的值,例如,只需使用myValues[0]获取第一个元素。

You can use a tuple but you don't have to. 您可以使用元组,但不必这样做。 It's just one way to do it. 这只是做到这一点的一种方法。 What you have in [] s is an array. 您在[]的是一个数组。 You can use that also and if you add a third+ website, you won't have to change the data structure, you'll just add a new value, eg x = [1, 2, 3] so having the array makes it easier to expand later vs a tuple. 您也可以使用它,并且如果您添加一个Third +网站,则不必更改数据结构,只需添加一个新值即可,例如x = [1, 2, 3]因此使用数组可以使操作变得更容易稍后扩展为元组。

Now to the code. 现在到代码。 It's kinda hard to understand your logic without more explanation but this should address your problem (and simplify your code). 在没有更多解释的情况下很难理解您的逻辑,但这可以解决您的问题(并简化您的代码)。

You will notice that both the if and else block have basically the same code. 您会注意到, ifelse块基本上具有相同的代码。 The only difference is "1" vs "0" in the send_keys() . 唯一的区别是send_keys()中的“ 1”与“ 0”。 You should do some reading on DRY and DRY practices but the core concept is Don't Repeat Yourself. 您应该阅读有关DRY和DRY惯例的内容,但核心概念是“不要重复自己”。 Pull the repeated lines out into a method called set_fulfillment_value() (You might want to give it a more appropriate name). 将重复的行拉出到名为set_fulfillment_value()的方法中(您可能想给它一个更合适的名称)。

def set_fulfillment_value(value)
    LineItem.find_element_by_xpath('./td[4]/div/div/input').clear()
    # time.sleep(2) # this is a bad practice and should be removed (it likely isn't needed here anyway)
    LineItem.find_element_by_xpath('./td[4]/div/div/input').send_keys(value)

Use the if to set the value to be sent and then pass that into the method. 使用if设置要发送的值,然后将其传递给方法。

x = [1, 2]
value = None
if Website1 == x[0]:
    value = "1"
else:
    value = "0"
set_fulfillment_value(value)

NOTE: I know naming is hard but you should work on better variable names. 注意:我知道命名很困难,但是您应该使用更好的变量名。 Website1 sounds like it might be better named something like product ? Website1听起来好像可以更好地命名为product Also, LineItem isn't very python-y. 另外, LineItem也不是python-y。 line_item would probably be better. line_item可能会更好。

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

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