Empty function x while using return [duplicate] - function

This question already has answers here:
Why does my recursive function return None?
(4 answers)
Asking the user for input until they give a valid response
(22 answers)
Closed 11 days ago.
def symbol():
calc_symbol = input("def symbol operation")
if calc_symbol == "+" or calc_symbol =="-" or calc_symbol == "*" or calc_symbol =="/":
print("Super jdeme dál")
return calc_symbol
else:
print("You mage mistake insert symbol again")
symbol()
x = symbol()
print(f"This is operation symbol {x}")
Hello,
Please help.
If I don't make a mistake f-cion retur give mi symbol.
But
When I make a mistake. Return doesn't give me symbol but give me non.
How can I write a function so that it returns a character even during correction?
Thank you

Related

Calling #staticmethod function on Python tkinter Button click [duplicate]

This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 6 months ago.
Why is not working? PyCharm message: Expected type 'Union[str, () -> Any]', got 'None' instead. I do not understand.
Part of a project. Thanks for the reply.
import tkinter as tk
class ClassA(tk.Tk):
def __init__(self):
super().__init__()
self.button = tk.Button(self, text="Start", command=ClassA.a_method())
self.button.pack()
#staticmethod
def a_method():
print('a')
if __name__ == '__main__':
app = ClassA()
app.mainloop()
Remove parenthesis bracket in line 9.
Change this:
self.button = tk.Button(self, text="Start", command=ClassA.a_method())
to:
self.button = tk.Button(self, text="Start", command=ClassA.a_method)

RegEx for capturing an attribute value in a HTML element [duplicate]

This question already has answers here:
Extract Title from html link
(2 answers)
Closed 3 years ago.
I have a problem to extract text in the html tag using regex.
I want to extract the text from the following html code.
Google
The result:
TEXTDATA
I want to extract only the text TEXTDATA
I have tried but I have not succeeded.
Here we want to swipe the string up to a left boundary, then collect our desired data, then continue swiping to the end of string, if we like:
<.+title="(.+?)"(.*)
const regex = /<.+title="(.+?)"(.*)/gm;
const str = `Google`;
const subst = `$1`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
RegEx
If this expression wasn't desired, it can be modified or changed in regex101.com.
RegEx Circuit
jex.im also helps to visualize the expressions.
PHP
$re = '/<.+title="(.+?)"(.*)/m';
$str = 'Google';
$subst = '$1';
$result = preg_replace($re, $subst, $str);
echo $result;
Use this regex:
title=\"([^\"]*)\"
See:
Regex
Google
Remvoe Title and try

can anyone explain to me why the following two arrow functions are equivalent? [duplicate]

This question already has answers here:
What do curly braces inside of function parameter lists do in es6?
(3 answers)
Closed 5 years ago.
I am pretty new to javascript. saw this on MDN regarding arrow functions.
Can anyone explain to me how does the 2nd one work? I understand the first one.
Not quite sure why we put length in an object, and then return the length???
Case 1 (which i understand from how it transformed from ES5):
materials.map((material) => {
return material.length;
}); // [8, 6, 7, 9]
Case 2 (not getting what {length} is doing here and also why do we return length:
materials.map(({length}) => length); // [8, 6, 7, 9]
Thank you so much!
Update:
So reading from the answer from Jeff B. It appears that the 2nd one is doing the following with destructuring:
materials.map(({length}) => length)
in which {length} will set a variable var length to equal to materials.length; and that's why we can simply return length. That makes sense. Thanks Jeff
This uses a destructuring assignment to get at the length property without saving the whole object. Specifically, this is a case of "object destructuring".
For instance:
let yourObject = {foo: 1, bar: 2}
let {bar} = yourObject;
// bar now equals 2
With this in mind, you can see how ({length}) => length sets the name length to the length property of the first parameter and then immediately returns it—making the two expressions equivalent.

How should you handle nested functions? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
What's the proper way to call a function in PowerShell?
Example 1:
Function Divide
{
Return ($a / $b)
}
Function GetNumbers
{
$a = Read-Host "Dividend"
$b = Read-Host "Divisor"
Divide
}
GetNumbers
Example 2:
Function Divide
{
Param
(
[Int]$Dividend,
[Int]$Divisor
)
Return ($Dividend / $Divisor)
}
Function GetNumbers
{
$a = Read-Host "Dividend"
$b = Read-Host "Divisor"
Divide -Dividend $a -Divisor $b
}
GetNumbers
I mean, why should I use the Param part when I can access the parent function's variables? Or is that just bad programming?
The problem with the first example is that your Divide function will only work if it happens to be inside the GetNumbers function. If you try to call it outside of GetNumbers, you will raise an error because $a and $b will be undefined:
PS > Divide
Attempted to divide by zero.
At line:3 char:12
+ Return ($a / $b)
+ ~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException
This means the use case of your Divide function is very limited and also somewhat unintuitive. People may call the function thinking it will work (since it is at module-level) and be surprised by the results.
The Divide function in the second example however does not have this issue. It is independent of the GetNumbers function and can be used anywhere a normal function can. This has three advantages:
Divide behaves as a module-level function should, which means there are no nasty surprises for your users.
The use case of Divide has been greatly enhanced. You can now divide two numbers anywhere you need to.
Your code is more robust. Divide does not need to be inside a function which just happens to define two variables named $a and $b which just happen to be numbers (see how fragile the first example is?).
Note too that you do not need to explicitly give the parameter names in your second example. You could just do:
Divide $a $b
And if you dislike the size of the second Divide function, you could always make your Param statement less spread out:
Function Divide
{
Param ([Int]$Dividend, [Int]$Divisor)
Return ($Dividend / $Divisor)
}

Function callback ('StopFcn' , 'TimerFcn' )for audiorecorder object in MATLAB? [duplicate]

This question already exists:
Closed 10 years ago.
Possible Duplicate:
How can I use function callback ('StopFcn' , 'TimerFcn' )for audiorecorder object in MATLAB?
So I try to this code.
% assume fs,winsize,winshift is given.
T = 0.1; % in seconds
samples = cell{100,1};
r = audiorecorder(fs,16,1);
k=1;
r.TimerPeriod = 0.1;
r.StopFcn = 'samples{k} = getaudiodata(r);';
r.TimerFcn = {#get_pitch,samples{k},winsize,winshift};
while 1
record(r,T);
k=k+1;
end
I want to execute the function 'get_pitch(samples,fs,winsize,winshift)' while during recording through audiorecorder object.
But following exception occurs during execution.
1) after record(r,T) is executed. (StopFcn is now called) ??? Error using ==> eval Undefined function or variable 'r'.
2) after StopFcn is called (TimerFcn is now called) In this phase, get_pitch function have totally wrong parameters. For example, parameter in the position samples{k} change to 'audiorecorder object'.
It seems that I do not know exact use of 'StopFcn' & 'TimerFcn'.
Is there anyone who can give me some advice? I really appreciate all of your comments.
Looking at the example in the documentation I would recommend trying to call getaudiodata(r) in your loop rather than with the CallBack. So something like this:
% assume fs,winsize,winshift is given.
T = 0.1; % in seconds
samples = cell{100,1};
r = audiorecorder(fs,16,1);
k=1;
r.TimerPeriod = 0.1;
r.StopFcn = 'disp(''Completed sample '', k)';
r.TimerFcn = {#get_pitch,samples{k - 1},winsize,winshift};
while 1
record(r,T);
samples{k} = getaudiodata(r);
k=k+1;
end
Note I changed the r.TimerFcn to use samples{k - 1} instead of k because k will increment before the timerfcn gets called. So this might give you issues with your first sample, you'll have to tweak it a little. Also this is an infinite loop, which I'm sure you'll want to address.