Function runs twice in console (python3, eclipse) - eclipse

Hi! Could you please explain why the function runs twice in console?
def changeList(myList1):
myList2 = myList1.append(4)
print(myList2)
return
myList1 = [1,2,3]
changeList(myList1)
print (myList1)
The result in console:
None
[1, 2, 3, 4]
Does it mean function runs twice as "None" appears in the console?

tl;dr - the function is only running once -- there are two print statements producing output
The function is not running twice: indeed, it is only being run once. The output in the console is instead coming from the two calls to print() contained within your program: one inside the function changeList() and one outside the function (print(myList1)).
None is being printed to the console because the return statement within the function changeList() isn't returning anything - there is no value to return:
If an expression list is present, it is evaluated, else None is
substituted.
[Taken from the Python 3.6 Documentation]
Seeing as how the return statement isn't doing anything, you can safely remove it - the function will still end anyway.
Hope that helps you out!

The function is running only once. You are appending one item to list and tried to store in other list by just assigning list with one more item appending which returns None and assigns to myList2. So, the code is wrong because append() function return's None.
I think you wan't to do like this so, here is the correct code:
comment if is it solved your problem or not.
def changeList(myList1):
myList2=[]
myList2.extend(myList1)
myList2.append(4)
print(myList2)
return
myList1 = [1,2,3]
changeList(myList1)
print (myList1)

Because in the function definition of changeList, there is a print statement, and then another print statement after calling changeList. The function is only running once actually, but you simply have two separate print statements.

Related

Powershell script queues results of an if statement in a do while in a function

I'm using a function that I call from another script. It prompts a user for input until it gets back something that is not empty or null.
function GetUserInputValue($InputValue)
{
do{
$UserValue = Read-Host -Prompt $InputValue
if (!$UserValue) { $InputValue + ' cannot be empty' }
}while(!$UserValue)
$UserValue
return $UserValue
}
The issue is quite strange and likely a result of my lack of powershell experience. When I run the code and provide empty results, the messages from the if statement queue up and only display when I finally provide a valid input. See my console output below.
Console Results
test:
test:
test:
test:
test:
test:
test: 1
test cannot be empty
test cannot be empty
test cannot be empty
test cannot be empty
test cannot be empty
test cannot be empty
1
I can make this work however in the main file with hard coded values.
do{
$Server = Read-Host -Prompt 'Server'
if (!$Server) { 'Server cannot be empty' }
}while(!$Server)
I'm working Visual Studio Code. This is a function I have in another file I've named functions.ps1.
I call this from my main file like this,
$test = GetUserInputValue("test")
$test
When you put a naked value in a script like "here's a message" or 5 or even a variable by itself $PID what you're implicitly doing is calling Write-Output against that value.
That returns the object to the pipeline, and it gets added to the objects that that returns. So in a function, it's the return value of the function, in a ForEach-Object block it's the return value of the block, etc. This bubbles all the back up the stack / pipeline.
When it has nowhere higher to go, the host handles it.
The console host (powershell.exe) or ISE host (powershell_ise.exe) handle this by displaying the object on the console; this just happens to be the way they handle it. Another host (a custom C# application for example can host the powershell runtime) might handle it differently.
So what's happening here is that you are returning the message that you want to display, as part of the return value of your function, which is not what you want.
Instead, you should use Write-Host, as this writes directly to the host, skipping the pipeline. This is the correct command to use when you want to display a message to the user that must be shown (for other information you can use different commands like Write-Verbose, Write-Warning, Write-Error, etc.).
Doing this will give you the correct result, and prevent your informational message from being part of the return value of your function.
Speaking of which, you are returning the value twice. You don't need to do:
$UserValue
return $UserValue
The first one returns the value anyway (see the top of this answer); the second one does the same thing except that it returns immediately. Since it's at the end of the function anyway, you can use wither one, but only use one.
One more note: do not call PowerShell functions with parentheses:
$test = GetUserInputValue("test")
This works only because the function has a single parameter. If it had multiple params and you attempted to call it like a method (with parentheses and commas) it would not work correctly. You should separate arguments with spaces, and you should usually call parameters by name:
$test = GetUserInputValue "test"
# better:
$test = GetUserInputValue -InputValue "test"

pytest function without return value

I'm trying to do a pytest on a function without return value, but obviously value is None in pytets. I was wondering if there is a solution for that?
here is function which I'm trying to test:
def print_top_movies_url():
for item in movie_link[:100]:
print item.contents[1]
The best thing to do would be to separate getting the top movies and printing them.
For example, you could have a top_movie_urls which looks like this:
def top_movie_urls():
urls = []
for item in movie_link[:100]:
urls.append(item.contents[1])
return urls
(or make it a generator function)
That's easy to test, and wherever you call it, you can now just do something like print('\n'.join(top_movie_urls())).
If you really want to test the output instead, you can use pytest's output capturing to access the output of the tested function and check that.

Multiple Conditions in one expect

I want to Verify text of AssignedAllUsers List can contains test1 or test2 but it should not contain test3
I am using following but not sure what is the problem with the code I am getting following error : AllAssignee.toContain is not a function
this.IncList = element.all(by.repeater("incident in $ctrl.allIncidents"));
this.AssignedAllUsers = this.IncList.all(by.css('[aria-label="Change assignee to "]'));
AssignedAllUsers.getText().then(function(AllAssignee){
console.log("AllAssignee = "+AllAssignee);
expect((AllAssignee.toContain(Logindata.Username0)) || (AllAssignee.toContain(Logindata.Username1)) && (AllAssignee.not.toContain(Logindata.Username2)));
});
Your error is a syntax issue. toContain belongs outside the value being tested, in other words outside of the first set of parentheses following your expect statement.
You have this:
expect((AllAssignee.toContain(Logindata.Username0)). You also have an extra set of parentheses, though I don't think that really matters.
You need to close the AllAssignee call, it should be: expect(AllAssignee).toContain(Logindata.Username0)
To answer your other question, there's no need to do it in one expect statement really. Since the list should never contain test3, thats your first assertion:
expect(AllAssignee).not.toContain(test3);
As for your other expected values, if you do not know which one will be present, just create an array and put both possible values inside of that. Then you can assert against the array to contain either test1 or test2:
var myArray = ['test1', 'test2'];
expect(myArray).toContain(AllAssignee);
Also see this related question about expecting items items in an array
Able to fix the problem with the code:
expect(AllAssignee).toContain('test1' || 'test2');

No output for map function in playground

I have these very simple lines of code:
var friends = ["Mike", "Marika", "Andreas", "Peter", "Sabine"]
friends.map{
println("Hallo \($0)!")
}
This works fine in a program but I get no output in a playground.
It only tells me the count of the elements and how many times to function needs to run. But it does not write the strings.
Is it me or is this a bug in Xcode?
It's not a bug in Xcode. While your map code will print to the standard out (hit cmd-opt-enter to reveal the output in the assistant editor on the right), stylistically you should avoid using map for this. You would be better off with a for...in loop:
for friend in friends {
println("Hallo \(friend)")
}
If you quick-look the results this time, you'll see a more helpful result:
(note, I've switched the quick look to the list view, which shows every result, rather than just the last one)
Why is this working differently? It's because map isn't really for running arbitrary code against your array. It's more specifically for transforming (i.e. mapping) your array into another array. So suppose instead of printing a list of friends, you wanted a list of greetings, you could do this:
let greetings = friends.map { friend in
"Hallo \(friend)"
}
greetings will now be set to a new array of 5 strings, one for each name, of the form "Hallo <name>". map is taking a closure that takes a string, and maps it to a new string.
So what is happening when you write friends.map { println("...") } is that map is calling the closure, and getting the result of the expression in the closure, and populating a new array with it. Since println returns Void, the result of that expression each time is Void. And it is that Void that Xcode is displaying (as "(0 elements)" which is how the UI displays Void).
Instead, with the for-loop, Xcode knows that a stand-alone println who's value isn't being used should be interpreted not as a result, but as what got output to the standard out, so that's what it does.

return of parameters from matlab script

let us suppose that we have following function
function [y1,y2,y3,y4]=mystery(a,x);
y1=a*x;
y2=a*x^2;
y3=a^2*x^2;
y4=a*x^3+5;
end
now what i want to make sure is order of result returned from this code,for instance
[y1,y2,y3,y4]=mystery(3,5);
does it return in reverse order or directly in direct form?i meant when m file is executed does it first return last result,then previous of last line and so on?thanks in advance
The parameters are always returned in the order of the declaration. The order of evalutation does not matter. So, in your case, you will always have the order [y1,y2,y3,y4].
Edit:
If you want to access the second or third parameter only, you can do [~,y2]=mystery(1,2) or [~,~,y2]=mystery(1,2) respectively.
Matlab executes line by line in the script. The first line is always executed first.