Go through the list of fruits and print each one in the string “I added this to our produce”. The parameter is "a list of crop names (strings) and there is no return value.
Test Case
crops = [‘apple’, ‘orange’, ‘banana’, ‘strawberry’]
daysWork(crops)
I added this apple to our produce
I added this orange to our produce
I added this banana to our produce
I added this strawberry to our produce
My Code:
def daysWork(crops):
for crop in crops:
produce = crops[n]
print 'I added this ' + str(produce) + ' to our produce'
The error I get is "The error was:n Name not found globally."
Is n not indexing the list at each element?
Python's for loop statement is actually a for-each statement, which differs from what you're used to in C or Java. for crop in crops references each element in crops with the variable crop.
What you want instead is:
for crop in crops:
produce = crop
Related
I am trying to write a code which calculates the HCF of two numbers but I am either getting a error or an empty list as my answer
I was expecting the HCF, My idea was to get the factors of the 2 given numbers and then find the common amongst them then take the max out of that
For future reference, do not attach screenshots. Instead, copy your code and put it into a code block because stack overflow supports code blocks. To start a code block, write three tildes like ``` and to end it write three more tildes to close. If you add a language name like python, or javascript after the first three tildes, syntax highlighting will be enabled. I would also create a more descriptive title that more accurately describes the problem at hand. It would look like so:
Title: How to print from 1-99 in python?
for i in range(1,100):
print(i)
To answer your question, it seems that your HCF list is empty, and the python max function expects the argument to the function to not to be empty (the 'arg' is the HCF list). From inspection of your code, this is because the two if conditions that need to be satisfied before anything is added to HCF is never satisfied.
So it could be that hcf2[x] is not in hcf and hcf[x] is not in hcf[x] 2.
What I would do is extract the logic for the finding of the factors of each number to a function, then use built in python functions to find the common elements between the lists. Like so:
num1 = int(input("Num 1:")) # inputs
num2 = int(input("Num 2:")) # inputs
numberOneFactors = []
numberTwoFactors = []
commonFactors = []
# defining a function that finds the factors and returns it as a list
def findFactors(number):
temp = []
for i in range(1, number+1):
if number%i==0:
temp.append(i)
return temp
numberOneFactors = findFactors(num1) # populating factors 1 list
numberTwoFactors = findFactors(num2) # populating factors 2 list
# to find common factors we can use the inbuilt python set functions.
commonFactors = list(set(numberOneFactors).intersection(numberTwoFactors))
# the intersection method finds the common elements in a set.
I would like some assistance on writing some Lua code in which if a user inputs a color for a car the code will be able to search a lookup table to see if that color is included in the lookup table as a keyword and will be able to output a score which defines the result for example 1 = accept, 2 = decline deepening on what color car they have typed at the beginning.
Thank you
This is a rather trivial program in lua. because lua allows for simple creation of associative arrays(a.k.a. hash tables) you can quickly make a lookup table.
local carColors = {
purple = "1"
}
From there you index the table with your user input and return your 1 or 2
local userInput = io.read():lower() --Make sure to set the user input to all lowercase.
print(carColors[userInput] or "2") -- if nil return 2
I used print rather than io.output.
the or here lets the code handle when the user gives a bad color name when carColors[userInput] is nil the 2 will be printed.
Hallo again dear forum,
I am not the best of friends with these 3D plots, and I struggle with simple formatting stuff. Like now, where I can't color my plots from a variable.
with(samples3d, {
s3d <- scatter3D(MDS2, MDS3, MDS1, pch = ifelse(meta$op.closed=="cl",22,21), type = "h",colvar = pcolor, lty.hplot=2, scale.y=0.75)
} )
It gives me this error:
Error in clim[2] - clim[1] : non-numeric argument to binary operator
I can read from the documentation that:
"colvar :The variable used for coloring. ... if
specified, it should be a vector of equal length as (x, y, z)."
So in my naïve approach I checked
colvec <- as.vector(samples3d$pcolor)
MDS1vec <- as.vector(samples3d$MDS1)
length(MDS1vec)
43
length(colvec)
43
- and they are the same length, so what is wrong here?
Best,
Mathilde
I also find the colouring scheme a bit difficult. But colvar should be a numeric vector, 1,2,3,4 up to number of groups you have. And then you have to supply col as another vector - having the length of the amount of groups. Scatter3D will then look up each of your numbers in that other vector, supplied as the argument 'col'. E.g.:
colvar <- as.numeric(as.factor(pcolor))
Now all your colours are made into numbers. And then:
col <-levels(as.factor(pcolor))
That's the col argument where the function can get the colours.
Without knowing what kind of data is in your vectors, I would assume from the error message that your pcolor vector is non-numeric.
A detailed answer to this error message is given here:
Non-numeric Argument to Binary Operator Error in R
A solution could be to code your groups in pcolor with numeric values. At least that worked for me, when I had this problem.
I apologize if this has been asked before (I couldn't find anything).
I'm an extreme noob in Livecode, and I want to know if there is a way of programming a button to create many new, unique variables and assign a value to them. I apologize if this is a dumb question.
Usually you use an array for that. An array is basically a list of things, where each thing is associated with an "index". An index can be any word, so you can use an array like a dictionary, where you'd e.g. have French words as the index, and English words as the value, like:
put "cow" into myDictionary["vache"]
But you can also just use numbers as the keys and make them a numbered list:
put "cow" into allMyAnimals[1]
put "duck" into allMyAnimals[2]
In end effect, you create one variable and put several things in it. For example if you had a loop that calculated something (in this example a number +100) and you wanted to have variables containing all those numbers, but named with 100 less, you'd do something like:
repeat with x = 1 to 250
put x +100 into twoHundredFiftyNumbersFrom101[x]
end repeat
And to read the first one:
answer "the first number is" && twoHundredFiftyNumbersFrom101[1]
Or all of them:
repeat with x = 1 to 250
answer twoHundredFiftyNumbersFrom101[x]
end repeat
Or whatever. You could also use 'do' to build the lines of code as a string, but then you have to make sure your variable names are generated in a fashion that makes them valid identifiers (e.g. have no spaces in them, no special characters). An array key can be any valid string, and the compiler can optimize them, and you can treat them as a whole and pass them between handlers.
Or you can do this "in the clear" with a "do" construction:
on mouseUp
repeat with y = 1 to 10
get random(100)
do "put it into onTheFlyVariable" & y
end repeat
end mouseUp
Step through this handler and watch the variables assemble themselves.
I have a datafile with multiple columns, the first two indicating the position and the others indicating other properties (such as number of items sent from this point). eg:
1 1 1 57.11
2 1 2 62.40
3 4 1 31.92
What I want to do is plot the points at the positions, but use values from the other columns to vary point type and size (for example). However I can't seem to find a way to reference columns in the plot. I know of the use of "variable", but I cant find a way to use multiple variables.
What I want is something like the following:
plot "mydata" using 1:2 notitle with points pt ($3) ps ($4/10)
so that pt and ps use the value for each point taken from the third and fourth columns respectively.
Is this even possible in gnuplot? Is there some sort of work-around?
You should be able to use the keyword variable to do something like this:
plot 'datafile' using 1:2:3:4 w points ps variable lc variable
Or possibly mapping the value to a palette:
plot 'datafile' using 1:2:3:4 w points ps variable lc palette
The keyword variable and/or palette causes gnuplot to read the properties from the file and they both require an extra column to be read via using. Of course all the usual stuff with using applies -- You can apply transforms to the data, etc:
plot 'datafile' using 1:2:3:($4+32.) w points ps variable lc palette
I don't remember off the top of my head whether the 3rd column will be the pointsize or the color here, and I don't have time right now to play around with it to figure it out. You can do the experimenting and post a comment, or I'll come back to this when I have time and add an update.
Some of the other properties (e.g. pointtype) can't be changed quite to easily using variable. The easiest way to do this is to use filters with the gnuplot ternary operator.
First, write a function that returns a pointtype based on the data from 1 column of the datafile:
my_point_type(x) = x
Here I use a simple identity function, but it could be anything. Now, you can loop over the pointtypes you want (here 1-10) making a plot for each:
plot [for PT=1:10] 'datafile' u 1:((my_point_type($3) == PT) ? $2:NaN) with points pt PT
This assumes that the column with pointtype information is the 3rd column and that the second column holds the position information. This can also be combine with the stuff that I demonstrated above.