I am trying to make a menu calculator in which the user inputs items and the program will add up the order numbers and output the cost. I have done some of the code already but in the function in says can't assign to literal.
itemlist=["1","2","3","4","5","6","7","8","9"]
def itemcost():
1=3.50 #can't assign literal error is here
2=2.50
3=4.00
4=3.50
5=1.75
6=1.50
7=2.25
8=3.75
9=1.25
return itemcost
order=int(input("Enter order"))
while items in order:
itemcost+str(order)
First, some good information to put in a question is the language and platform you are using. Your error comment in the code IS helpful, however.
What your code is trying to do is assign the value 3.50 to the VALUE 1. You can't change the value of pure numbers for obvious reasons. What I think you want is:
itemlist["1"]=3.50
On line 4 (and lines 5-12 after it) the 1 is read as the value one, i.e. a literal value. If you want to assign the value 3.50 to a variable, you will need to name the variable something that cannot be interpreted as a number and does not begin with a number, such as _1 or var1.
Related
I have a csv, which looks like this:
name,code,age
Himsara,9877,12
John,9437721,16
Razor,232,45
I have to replace the column code according to some regular expressions. My logic is shown in a Scala code below.
if(str.trim.length == 9 && str.startsWith("369")){"PROB"}
else if(str.trim.length < 8){"SHORT"}
else if(str.trim.startsWith("94")){"LOCAL"}
else{"INT"}
I used a UpdateRecord Processor to replace the data in the code column. I added a property called /code which contains the value.
${field.value:replaceFirst('^[0-9]{1,8}$','SHORT'):replaceFirst('[94]\w+','OFF_NET')}
This works when replacing code's with
length less than 8 with "SHORT"
starting with 94 with "LOCAL"
I am unable to find a way to replace data in the column, code when it's equal to 8 digits AND when it starts with 0. Also how can I replace the data if it doesn't fall into any condition mentioned above. (Situation which the data should be replaced with INT)
Hope you can suggest a workflow or value to be added to the property in Update record to make the above two replacements happen.
There is a length and startsWith functions.
${field.value:length():lt(8):ifElse(
'SHORT', ${field.value:startsWith(94):ifElse(
'LOCAL', ${field.value:length():equals(9):and(${field.value:startsWith(369)}):ifElse(
'PROB', 'INT'
)})})}
I have put the line breaks for easy to recognize the functions but it should be removed.
By the way, the INT means that some string values to replace? Sorry for the confusion.
Well, if you want to regular expression only, you can try the below code.
${field.value
:replaceFirst('[0-9]{1,8}', 'SHORT')
:replaceFirst('[94]\w+', 'OFF_NET')
:replaceFirst('369[0-9]{6}', 'PROB')
:replace(${field.value}, 'INT')
}
How does one capture a value recursively with regex, where value is a part of a group that repeats?
I have a serialized array in mysql database
These are 3 examples of a serialized array
a:2:{i:0;s:2:"OR";i:1;s:2:"WA";}
a:1:{i:0;s:2:"CA";}
a:4:{i:0;s:2:"CA";i:1;s:2:"ID";i:2;s:2:"OR";i:3;s:2:"WA";}
a:1 stands for array:{number of elements}
then in between {} i:0 means element 0, i:1 means element 1 etc.
then the actual value s:2:"CA" means string with length of 2
so I have 2 elements in first array, 1 element in the second and 4 elements in the last
I have this data in mysql database and I DO NOT HAVE an option to parse this with back-end code - this has to be done in mysql (10.0.23-MariaDB-log)
the repeating pattern is inside of the curly braces
the number of repeats is variable (as in 3 examples each has a different number of repeating patterns),
the number of repeating patterns is defined by the number at 3rd position (if that helps)
for the first example it's a:2:
and so there are 2 repeating blocks:
i:0;s:2:"OR";
i:1;s:2:"WA";
I only care to extract the values in bold
So I came up with this regex
^a:(?:\d+):\{(?:i:(?:\d+);s:(?:\d+):\"(\w\w)\";)+}$
it captures the values I want all right but problem is it only captures the last one in each repeating group
so going back to the example what would be captured is
WA
CA
WA
What I would want is
OR|WA
CA
CA|ID|OR|WA
these are the language specific regex functions available to me:
https://mariadb.com/kb/en/library/regular-expressions-functions/
I don't care which one is used to solve the problem
Ultimately I need this in as sensible form that can be presented to the client e.g. CA,ID,OR or CA|ID|OR
Current thoughts are perhaps this isn't possible in a one liner, and I have to write a multi-step function where
extract the repeating portion between the curly braces
then somehow iterate over each repeating portion
then use the regex on each
then return the results as one string with separated elements
I doubt if such a capture is possible. However, this would probably do the job for your specific purpose.
REGEXP_REPLACE(
REGEXP_REPLACE(
REGEXP_REPLACE(str1, '^a:\\d+:\{', ''),
'i:\\d+;s:\\d+:\"(\\w\\w)\";',
'\\1,'
),
'\,?\}$',
''
)
Basically, this works with the input string (or column) str1 like
remove the first part
replace every cell with the string you want
remove the last 2 characters, ,}
and voila! You get a string CA,ID,OR.
Aftenote
It may or may not work well when the original array before serialised is empty (it depends how it is serialised).
I do not have a project or anything, I just wanted to know if this was possible.
Let's say I have a variable that is a string,
var code:String="hello there"
is there any way possible I could keep that variable the same, while using it for only the first 10 letters (or any number of letters)?
For example, if I had 2 dynamic textboxes, could I assign one the first four letters of the variable, and the other one the last four letters of the variable?
Also, could I recognize a charCode and make that the endpoint? For example, could I recognize when a space occurs, and do all letters before that?
Thanks in advance.
var parts:Array = code.split(" ");
Will do just like it says: split the string at all occurrences of the delimiter, which in this case is space.
So my company needs to send our i10n json file to a translator that can translate the thing into other languages.
Now the our system uses this file. Because of this we are able to make some "funky" statements that can be understood by our system but not by our translators when they extract the file.
For instance we have a case like this:
"CHOOSE": "{VALUE, select, 1{Vælg bruger} other{Fejl}}",
In the above example our system either takes Vælg bruger or Fejl
We also have something like this:
"HAS_MATERIAL": "Indeholder {{COUNT}} {{COUNT > 1 ? 'filer' : 'fil'}}",
Basicly the result of this would be Indeholder and if count is bigger than 1 filer else fil.
The last case we have is something like this:
"YOU_HAVE_NOTIFICATION": "You have { LENGTH } {LENGTH, select, 1{new notification} other{new notifications}}",
Again Length is a temp value and that then decides which translation to take.
So now its my job to make a regex for this file so we can get a list of all the words that need to be translated. and i am rather lost. the above 3 cases has different ways of approaching the wanted value.
i attempted with something like this:
{(.*?)}
With a global flag
However this doesnt work on all the cases.
Since there are some kind of "command language" (or two) involved this probably will fail at some point, but it handles your given examples:
{\w+,\s*select,\s*\w+\s*{([^}]*)}\s\w+\s*{([^}]*)}|{[^?{}]+\?\s*'([^']*)'\s*:\s*'([^']*)'\s*}]*}|^([^{}]+)|([^{}]+)$
It treats individual case one by one:
The SELECT statement
Inside braces, expect some expression followed by a ,, a command (in this case select), a ,, a case value and here we grab the text inside braces. Then expect some other case value and again - grab the text inside braces. I expect there will be case were there are more than two cases -> fail. (it can be expanded to handle more though)
Then the ternary operator
Inside braces, expect some expression followed by a ?, then grab the text inside single quotes**. Then expect a : and again - grab the text inside single quotes.
At the start of a line
grab all text up to {.
And end of line
grab anything after last }.
I guess this is far from complete. E.g. it won't handle text between "selects", and feels very fragile, but it might help you get started.
Check it out here at regex101.
I am a college student just learning HTML. The assignment that I have to complete is a simple grade calculator. There is a single button that calculates everything, and the values go to three empty text fields(total, average, and percentage). The problem that I am having is the total is calculated and shows up in the field, but the number is followed by [object]. The average field shows NaN and the percentage field remains empty. Here is my code.
<input type="button" value="Click to calculate"
onclick="fullmark = parseFloat(document.getElementById('fullBox').value);
science = parseFloat(document.getElementById ('scienceBox').value);
math = parseFloat(document.getElementById('mathBox').value);
computer = parseFloat(document.getElementById('computerBox').value);
english = parseFloat(document.getElementById('englishBox').value);
History = parseFloat(document.getElementById('historyBox').value);
total=science+math+computer+english+history;
average=(total/5);
percentage=(total/fullmark)*100;
document.getElementById('totalBox').value=total;
document.getElementById('averageBox').value=average;
document.getElementById('percentageBox').value=percentage;">
Since you wrote History as the variable name first, but then used history when computing/assigning the value for total, that is a different variable – identifiers in JavaScript are case sensitive.
The only reason you don’t get your script aborted with an error about an undefined variable at that point, is that window.history exists – it’s the object that keeps the current navigation history of the window. (All global variables are properties of the window object, so that’s what your use of history gets resolved to.) And since it’s an object, [object] is what you get as result when transferring it into a string context (which happens through the use of +, which is both the addition and the string concatenation operator).
Apart from that, writing all that code into an onclick handler is kinda terrible style. Go learn how to use proper functions for stuff like this next ;-)