So I am writing a program that will need to increase some variables by the amount that the user types. I thought a code like this would work:
var = 0
increase = input("Increase by: ")
var + increase
print(var)
But it just says that var is 0. I even tried making the input type int, int(input(""))
Still didn't work. Why won't it increase the variable?
Keep in mind, I am using Python 2.7
EDIT It was solved! I just made it var += increase
You need to understand the difference between a statement and an assignment.
You're making the statement var + increase, but its result is never used. You're basically asking the computer to evaluate it, but there is no side-effect (like printing it to the screen, sending it over the network or putting it somewhere in memory)
You can use the result of your statement in many ways, for example, you can print it directly:
print var + increase
Or you can assign it to a variable
var = var + increase
Even better, to a NEW variable:
result = var + increase
Actually, in python, if you make just a statement, it is not entirely lost, it's kept in a magic variable called "ans" which is denoted by an underscore (_).
So you could do
var + increase
print(_)
you're missing the setting of var. When you add var + increase it needs to be
var=var+increase.
return var or print(var)
basically, you're adding var to increase without doing anything with the resulting sum.
You need to update var. All you are doing in line 3 is assigning var + increase to ans. Try doing this:
var = 0
increase = input("Increase by: ")
var = var + increase
print var
Related
I am new to pinescript and have coded a simple indicator in trading view pinescript. I can get my buy and sell labels and also send alert when conditions are met.
I am trying to store the entry price as a variable when a sell or buy signal has been met. I also pass a var called pos which is 1 for sell and 2 for long.
i am trying to make the indicator send an alert when the entry price is above or below 20 pips. however my variable which is passed to the alert is being shadowed??? why??? im sure my code is correct?
var pos=0
var short_update = false
var entry_price = 0
if sellSignal
longLabel = label.new(
x=bar_index,
y=na,
text="Sell - "+ str.tostring(RoundUp(hlc3,2)),
xloc=xloc.bar_index,
yloc=yloc.abovebar,
color=color.red,
textcolor=color.white,
style=label.style_label_down,
size=size.normal)
entry_price=close
pos=1
alertcondition(sellSignal, "Short Alert", "Go short")
price_lower=entry_price-20
current_price=close
if pos==1 and current_price < price_lower
short_update=true
alertcondition(short_update, "Move Stop at BE", "BE")
above is my attempt at coding an alert condition if the entry price of a short is now 20 pips less...not working? please help
Actually all your variables defined with var on top are being shadowed not only "the one" you're referring to.
Shadowing variable 'pos' which exists in parent scope. Did you want to
use the ':=' operator instead of '=' ?
What does it mean?
You have declared your variables and assigned the first values correctly by the assignment operator: = in the global scope. If you want to update these values later on you'll need the reassignment operator though: :=.
The := is used to reassign a value to an existing variable. It says
use this variable that was declared earlier in my script, and give it
a new value.
source
So in your case var pos=0, var short_update = false, var entry_price = 0 are not getting updated through your whole script, are jut being redeclared as new variables in your local scopes that have no effect on the global ones, though having the same name.The solution is as simple as the error message says: use := instead of = if you want to update a declared variable.
Final note: try updating eg.: pos like this pos=1 in the global scope below you var pos=0 declaration. You'll get an error saying "'pos' is already defined". The same applies to your example, the only difference is that it is not illegal to redeclare a variable in a local scope, but Pine Script warns you correctly that it's probably not what you wanted.
My sheet is a query-sheet from database. Some of them contain html hex-code color which I need to manually use edit>Find and Replace every time it is refreshed.
I am very new to Google App Script and been trying to use the following code:
function Clearcode() {
var lookupone = new RegExp(/{color:#.{7}/);
var rep = "";
var spreadSheet = SpreadsheetApp.getActive();
var querySheet = spreadSheet.getSheetByName("QUERY");
var lastRow = querySheet.getLastRow();
var lastColumn = querySheet.getLastColumn();
var data = querySheet.getRange(2, 1, lastRow-1, lastColumn).getValues();
var textfinder = querySheet.createTextFinder(lookupone);
var found = textfinder.replaceAllWith(rep);
return (found);
}
Yet, when I run this function in the sheet it did not work. Any thought?
P.S. I planned to eliminated "[color]" part of the hex-code as well by create the similar function.
P.S.2 I have attached a snapshot of a table as you requested. The red line is just for confidentiality of the data. Below the line is just a normal text.
Pay attention to types!
CreateTextFinder accepts String as argument NOT a regexp object.
To use strings as regular expressions, useRegularExpressions needs to be set to true
querySheet.createTextFinder("\\{color:#?.{0,6}\\}")//only 6 characters
.useRegularExpressions(true)
.replaceAllWith("")
I have a CouchDB database which has documents with the following format:
{ createdBy: 'userId', at: 123456, type: 'action_type' }
I want to write a view that will give me how many actions of each type were created by which user. I was able to do that creating a view that does this:
emit([doc.createdBy, doc.type, doc.at], 1);
With the reduce function "sum" and consuming the view in this way:
/_design/userActionsDoc/_view/userActions?group_level=2
this returns a result with rows just in the way I want:
"rows":[ {"key":["userId","ACTION_1"],"value":20}, ...
the problem is that now I want to filter the results for a given time period. So I want to have the exact same information but only considering actions which happened within a given time period.
I can filter the documents by "at" if I emit the fields in a different order.
?group_level=3&startkey=[149328316160]&endkey=[1493283161647,{},{}]
emit([doc.at, doc.type, doc.createdBy], 1);
but then I won't get the results grouped by userId and actionType. Is there a way to have both? Maybe writing my own reduce function?
I feel your pain. I have done two different things in the past to attempt to solve similar issues.
The first pattern is a pain and may work great or may not work at all. I've experienced both. Your map function looks something like this:
function(doc) {
var obj = {};
obj[doc.createdBy] = {};
obj[doc.createdBy][doc.type] = 1;
emit(doc.at, obj);
// Ignore this for now
// emit(doc.at, JSON.stringify(obj));
}
Then your reduce function looks like this:
function(key, values, rereduce) {
var output = {};
values.forEach(function(v) {
// Ignore this for now
// v = JSON.parse(v);
for (var user in v) {
for (var action in v[user]) {
output[user][action] = (output[user][action] || 0) + v[user][action];
}
}
});
return output;
// Ignore this for now
// return JSON.stringify(output);
}
With large datasets, this usually results in a couch error stating that your reduce function is not shrinking fast enough. In that case, you may be able to stringify/parse the objects as shown in the "ignore" comments in the code.
The reasoning behind this is that couchdb ultimately wants you to output a simple object like a string or integer in a reduce function. In my experience, it doesn't seem to matter that the string gets longer, as long as it remains a string. If you output an object, at some point the function errors because you have added too many props to that object.
The second pattern is potentially better, but requires that your time periods are "defined" ahead of time. If your time period requirements can be locked down to a specific year, specific month, day, quarter, etc. You just emit multiple times in your map function. Below I assume the at property is epoch milliseconds, or at least something that the date constructor can accurately parse.
function(doc) {
var time_key;
var my_date = new Date(doc.at);
//// Used for filtering results in a given year
//// e.g. startkey=["2017"]&endkey=["2017",{}]
time_key = my_date.toISOString().substr(0,4);
emit([time_key, doc.createdBy, doc.type], 1);
//// Used for filtering results in a given month
//// e.g. startkey=["2017-01"]&endkey=["2017-01",{}]
time_key = my_date.toISOString().substr(0,7);
emit([time_key, doc.createdBy, doc.type], 1);
//// Used for filtering results in a given quarter
//// e.g. startkey=["2017Q1"]&endkey=["2017Q1",{}]
time_key = my_date.toISOString().substr(0,4) + 'Q' + Math.floor(my_date.getMonth()/3).toString();
emit([time_key, doc.createdBy, doc.type], 1);
}
Then, your reduce function is the same as in your original. Essentially you're just trying to define a constant value for the first item in your key that corresponds to a defined time period. Works well for business reporting, but not so much for allowing for flexible time periods.
guys!
I'm new to this website and also not good with coding. So I would really appreciate some help.
Right now I'm in need of a specific code to make a google sheet work perfectly.
To further explain:
I have a google sheet that a few information will be input by other co-workers. What I need is a code that will register the date in a specific cell and by whom the input was made on another cell.
So far this is what I have:
function onEdit(event) {
var sheet = event.source.getSheetByName("Input");
// Note: actRng = return the last cell of the row modified
var actRng = event.source.getActiveRange();
var index = actRng.getRowIndex();
var cindex = actRng.getColumnIndex();
// Note: date = return date
// Note: user = return the user email
var userCell = sheet.getRange(index,14);
var dateCell = sheet.getRange(index,2);
var inputdate = Utilities.formatDate(new Date(), "GMT+0200", "yyyy-MM-dd");
// Note(with hour): var inputdate = Utilities.formatDate(new Date(), "GMT+0200", "yy-MM-dd HH:mm");
//var user = event.user; // Note: event.user will not give you collaborator's Id
var user = Session.getEffectiveUser();
// Note: setValue = Insert in the cell the date when this row was modified
if (userCell.Value == null) {
userCell.setValue(user);
dateCell.setValue(inputdate)
}
}
My main problems/questions are:
I don't exactly need the last modifier, but the person who first input info on the cells. Therefore I tried that last IF (If the cell that is supposed to have the last modifier e-mail is blank, it means that nobody changed that row before, so the code should add the user on the userCell), although it is not working since every change I make it ignores the verification.
I also want to add that the event will only happen if you add values, if you delete them, nothing happens. (so far even when I delete cells, it counts as modification)
Most of the sheet is protected to avoid that people by accident erase some of the formulas, so the cells that this code changes are also protected. Is there a way to make the code bypass cell protection?
Please, help me identify what I'm doing wrong and hopefully I'll get this working perfectly! Thanks for the help !
If you want to prevent the script from firing when a cell is deleted, try:
var editedCell = SpreadsheetApp.getActiveSheet().getRange(e.range.getRow(), e.range.getColumn());
if (editedCell == "") {
return;
}
I would change Session.getEffectiveUser() to session.getActiveUser().
The last if statement is unnecessary. You want whoever most recently edited the field to be identified, along with the date.
I'd like to track SyntaxNodes and SyntaxTrivias across
different versions of a Solution/Workspace.
I tried annotating some nodes with SyntaxAnnotations.
This works well as long as I don't update the workspace.
Calling Workspace.TryApplyChanges (successfully) seems to remove
all SyntaxAnnotations.
This surprised me. Why does this happen?
How can I track SyntaxNodes across workspace updates?
Example code follows:
var workspace = new AdhocWorkspace();
var project = workspace.AddProject("TestProject", LanguageNames.CSharp);
var klass = SyntaxFactory
.ClassDeclaration("Klass")
.WithAdditionalAnnotations(new SyntaxAnnotation("Foo"));
var compUnit = SyntaxFactory.CompilationUnit().AddMembers(klass);
var document = project.AddDocument("TestFile.cs", compUnit);
var docId = document.Id;
var solution = document.Project.Solution;
var root1 = document.GetSyntaxRootAsync().Result;
var klass1 = root1.GetAnnotatedNodes("Foo").FirstOrDefault();
var eq1 = klass1.IsEquivalentTo(klass); // returns true
var apply = workspace.TryApplyChanges(solution); // returns true
var root2 = workspace.CurrentSolution.GetDocument(docId).GetSyntaxRootAsync().Result;
var klass2 = root2.GetAnnotatedNodes("Foo").FirstOrDefault(); // returns null, why?
This happens because TryApplyChanges doesn't actually re-use your nodes as is. Instead it "replays" the same changes as textual changes to the actual solution, and then let's the parser re-parse.
This happens for a few reasons:
To avoid having annotations pile up over time in the trees and interfere with each other (consider something like that formatting or rename annotations used in CodeFixes still being present after the fix was applied).
To protect against trees that don't round-trip from showing up in CurrentSolution. It is possible to construct trees that the parser would never generate (consider changing operator precedence for example).
To ensure the changes are actually applied, requires changing the original representation - the files on disk or the text buffers in memory, not just using the new trees in the workspace.
You could consider using something like the SyntaxPath type from the Roslyn sources to try to find an equivalent node.