I have this section of code:
if storagetypecheck1 == "Virtual":
def storagecheck():
d = 1
dforid = str(1-d)
while d <= int(numcount):
storageidprefix = "specimen" + "[" + dforid + "]"
driver.find_element_by_id(storageidprefix + ".storageContainerForSpecimen").click()
pag.press('a')
pag.press('enter')
time.sleep(1)
d = d + 1
storagecheck()
storagecheck()
When the storage type of a webform is set to virtual, it will run and change the type to auto in the textboxes.
The problem is that it has to do so with multiple textboxes which follow the format specimen[x].storageContainerForSpecimen.
However, when I run this code, it just loops over and over without changing the value of d to 2, 3, etc.
I tried having d = 1 above the if statement, but then it says for line dforid = str(1-d) that d is not defined.
Where should I put the d = 1 expression so that it is able to be recognized by the storagecheck() loop while also being able to increase by 1 every loop?
storagecheck() calls itself recursively. Each time it calls itself, the line d = 1 is being executed, so the value of d is being reset. You need to place d outside of the function definition for d to continue incrementing, like so:
if storagetypecheck1 == "Virtual":
d = 1
def storagecheck():
global d
dforid = str(1-d)
while d <= int(numcount):
storageidprefix = "specimen" + "[" + dforid + "]"
driver.find_element_by_id(storageidprefix + ".storageContainerForSpecimen").click()
pag.press('a')
pag.press('enter')
time.sleep(1)
d = d + 1
storagecheck()
storagecheck()
We use the keyword global to introduce the variable d into the namespace of the function.
Related
What I need to do:
for each key in dictionary1 extract key1:value1 from dictionary1 and key1:value1 from dictionary2
assign those 2 pairs to 4 different variables
use those variables in other methods
move on to the next iteration (extract key2:value2 of both dictionaries 1 and 2, and assign to the same 4 variables)
Example:
d_one = {1:z, 2:x, 3:y}
d_two = {9:o, 8:n, 7:m}
the result has to be
a = 1
b = z
c = 9
d = o
(calling some other methods using those variables here)
(moving on to the next iteration)
a = 2
b = x
c = 8
d = n
(and so on)
My brain is overloaded on this one. Since I can't nest for loops to accomplish this task, I guess the correct usage of 'and' statement should do it? I have no idea how so I try to split it up...
d_one = {'1':'z', '2':'x', '3':'y'}
d_two = {'9':'o', '8':'n', '7':'m'}
for i in range(0, len(d_one)):
for a in list(d_one.keys())[i]:
a = d_one.keys()[i]
b = d_one[a]
for c in list(d_two.keys())[i]:
c = d_two.keys()[i]
d = d_two[c]
print(a, b, c, d)
output:
TypeError: 'dict_keys' object is not subscriptable
Try this:
d_one = {'1':'z', '2':'x', '3':'y'}
d_two = {'9':'o', '8':'n', '7':'m'}
for (a,b), (c,d) in zip(d_one.items(), d_two.items()):
print(a, b, c, d)
I ran into a problem when adding variables. Their values are glued together, but I need to calculate
a = check1()
b = check2()
c = check3()
d = check4()
g = check5()
e = check6()
f = check7()
g = сheck8()
i = сheck9()
online = a + b + c + d + g + e + f + g + i
functions are all identical:
def check1():
with valve.source.a2s.ServerQuerier(a) as server:
info = server.info()
players = server.players()
return '{player_count}'.format(**info)
I get as a result: 007220000
how to count them?
It would be of great help if you pasted the whole code segment here, including the functions. It seems to me that you are returning the values from those functions as strings.
So a typical way to assign arguments to a variable in python is:
variable_name = "argument name as a string"
or
variable_name = 1
With 'argv' or return in a function, the whole syntax is inverted:
argument_name1, argument_name2 = argv
or
def function_name(a):
a = 1
b = a + 1
return a, b
a, b = function(1)
What's the deal with this inversion? Is it something I need to remember, or is there a logic behind this that can be applied to more things?
There is no inversion in your examples.
The = sign assigns whatever is on the right of it to whatever is on the left to it.
Here, you assign values to a new variable:
variable_name = "argument name as a string"
variable_name = 1
In the function, you assign the output of the function (right) to the variables on the left:
def function_name(a):
a = 1
b = a + 1
return a, b
a, b = function_name(1)
If your confusion comes from the comma, it basically allows you to assign two variables at the same time.
function_name(1) returns (a,b)
a, b = function_name(1)
is simply short for:
output = function_name(1) # Output is (a, b)
a = output[0]
b = output[1]
However, I think you are confused about the general concept of assignment. The function takes an argument a, but then overrides the argument with 1.
It should be either:
def function_name():
a = 1
b = a + 1
return a, b
or
def function_name(a):
b = a + 1
return a, b
Here is how you can test your claim that you are assigning from left to right:
argument_name1 = 'a'
argument_name2 = 'b'
argument_name1, argument_name2 = argv
print argv
Will throw the error NameError: name 'argv' is not defined
But:
argv = ('a', 'b')
argument_name1, argument_name2 = argv
print(argument_name1, argument_name2)
prints: a b
The syntax is not inverted. Python actuallly allows you to unpack the variable on the fly. The function actually returns a tuple
def function_name(a):
a = 1
b = a + 1
return a, b
If you test the function return type, you get:
>>> type(function_name(1))
<type 'tuple'>
This syntax actually prevents you from wirting this
value = function_name(1) # value = (1, 2)
a = value[0] # a = 1
b = value[1] # b = 2
def sumOfStudentDigits():
studentdigit = (studentdigit1 + studentdigit2 + studentdigit3 + studentdigit4 + studentdigit5 + studentdigit6 + studentdigit7)
studentdigit1=3 studentdigit2=6 studentdigit3=9 studentdigit4=3
studentdigit5=1 studentdigit6=0 studentdigit7=0
I need to assign seven digits to seven variables and add them together.
If your confusion is how to get the studentdigits into your function, you can pass them into the function like this:
def sumOfStudentDigits(studentdigit1, studentdigit2, studentdigit3,
studentdigit4, studentdigit5, studentdigit6,
studentdigit7):
studentdigit = (studentdigit1
+ studentdigit2
+ studentdigit3
+ studentdigit4
+ studentdigit5
+ studentdigit6
+ studentdigit7)
My advice would be to have all those digits stored in a list, and then pass merely that list to the function, then iterate over the list:
listofdigits = [studentdigit1,
studentdigit2,
studentdigit3,
studentdigit4,
studentdigit5,
studentdigit6,
studentdigit7]
def sumOfStudentDigits(studentdigitlist):
sum = 0
for digit in studentdigitlist:
sum += digit
return sum
print(sumOfStudentDigits(listofdigits))
We have to set sum = 0 before we can use sum because python wants to know what sum is before it uses it, so we assign it 0 so that we can count up from there.
Notice how studentdigitlist and listofdigits are different?
You can pass a list of any name to the function, all that matters is that you use the variable (ie a list in this case) name that you have used in def myfunction(yourvariable): throughout the function definition. Python with substitute whatever you pass into the function for where you have that placeholder name within the function. Then when you run the function:
eg
def myfunction(yourvariable):
# do stuff with yourvariable
myvariable = myvariable + 7
somenumber = 2
myfunction(somenumber)
# now somenumber will equal 9
You could also pass in the entire student number and break it down inside of your function.
def sum_student_digits(student_id):
running_total = 0
for i in str(student_id):
running_total += int(i)
return running_total
print(sum_student_digits(12345))
Keeping things basic. You need to assign the seven digit student number, one to each variable.
def sumOfStudentDigits():
digit1 = 3
digit2 = 6
digit3 = 9
digit4 = 3
digit5 = 1
digit6 = 0
digit7 = 0
And then add them together:
print(digit1 + digit2 + digit3 + digit4 + digit5 + digit6 + digit7)
Note that the variable assignments can't be on the same line, and need to come before the sum.
Given the following input file:
a = 2
b = 3
c = a * b
d = c + 4
I want to run the above input file through a python program that produces
the following output:
a = 2
b = 3
c = a * b = 6
d = c + 4 = 10
The input file is a legal python program, but the output is python with
extra output that prints the value of each variable to the right of the
declaration/assignment. Alternatively, the output could look like:
a = 2
b = 3
c = a * b
c = 6
d = c + 4
d = 10
The motivation for this is to implement a simple "engineer's notebook"
that allows chains of calculations without needing print statements
in the source file.
Here's what I have after modifying D.Shawley's (much appreciated) contribution:
#! /usr/bin/env python
from math import *
import sys
locals = dict()
for line in sys.stdin:
line = line.strip()
if line == '':
continue
saved = locals.copy()
stmt = compile(line,'<stdin>','single')
eval(stmt,None,locals)
print line,
for (k,v) in locals.iteritems():
if k not in saved:
print '=', v,
print
Something like the following is a good start. It's not very Pythonic, but it is pretty close. It doesn't distinguish between newly added variables and modified ones.
#! /usr/bin/env python
import sys
locals = dict()
for line in sys.stdin:
saved = locals.copy()
stmt = compile(line, '<stdin>', 'single')
eval(stmt, None, locals)
print line.strip(),
for (k,v) in locals.iteritems():
if k not in saved:
print '=', v,
print