I am trying to loop round all of the number keys on a keyboard (0-9) and thought to do this you would use this code:
for i in range (0,10):
if keys[pygame.K_i]:
pass
But obviously as expected, pygame.K_i treats 'i' as the keyboard input 'i' not the variable. I was wondering how to specify I would like to use the variable not the keyboard input.
Sorry if the title is misleading, thanks.
The key constants (like pygame.K_0) are just integers that represent the keyboard keys. pygame.K_0 is 48, pygame.K_1 is 49 and so on. You can use the range range(48, 58).
Related
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.
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.
I have created a Google Form that has a field where a numeric value is entered by the user (with numeric validation on the form) and is then submitted. When the number (e.g., 34.00) gets submitted, it appears as 34 in the Google spreadsheet, which is annoying but understandable. I already have a script that runs when the form is submitted to generate a nicely-formatted version of the information that was submitted on the form, but I'm having trouble formatting that value as a monetary value (i.e., 34 --> $34.00) using the Utilities.formatString function. Can anyone help? Thanks in advance.
The values property of a form submission event as documented in Event Objects is an array of Strings, like this:
['2015/05/04 15:00', 'amin#example.com', 'Bob', '27', 'Bill', '28', 'Susan', '25']
As a result, a script that wishes to use any of these values as anything but a String will need to do explicit type conversion, or coerce the string to number using the unary operator (+).
var numericSalary = +e.values[9];
Alternatively, you could take advantage of the built-in type determination of Sheets, by reading the submitted value from the range property also included in the event. Just as it does when you type in values at the keyboard, Sheets does its best to interpret the form values - in this case, the value in column J will have been interpreted as a Number, so you could get it like this:
var numericSalary = e.range.getValues()[9];
That will be slower than using the values array, and it will still provide an unformatted value.
Formatting
Utilities.formatString uses "sprintf-like" formatting values. If you search the interwebs, you'll find lots of references for sprint variables, some of which are helpful. Here's a format that will turn a floating-point number into a dollar-formatted string:
'$%.2f'
$ - nothing magic, just a dollar sign
% - magic begins here, the start of a format
.2 - defines a number with two decimal places, but unspecified digits before the radix
f - expect a floating point number
So this is your simplest line of code that will do the conversion you're looking for:
var currentSalary = Utilities.formatString( '$%.2f', +e.values[9] );
The correct format to get your string into a proper numeric format is as follows:
var myString = myString.toLocaleString('en-US', { style: 'currency', currency: 'USD' });
If you only care about the $ sign and don't need commas the code below (also shown in one of the answers above will suffice.
var myString = Utilities.formatString( '$%.2f', myString );
In my experience toLocaleString performs sometimes performs strangely in Apps Script as opposed to JavaScript
I know this was years ago but might help someone else.
You should be able to add the commas in between with this
.toLocaleString(); after you format the string decimal then add the '$' sign in the beginning by just concating them together.
Ex:
myString = Utilities.formatString( '%.2f', myNumber );
myString = '$' + myString.toLocaleString();
I am trying to search a string from a specific point onward.
I'm looking for a r ether low case or upper case then finding the dash after the r's location. I can do this in php but in ActionScript 3 string.search always starts at the beginning of the string. Is there an alternative that works more like stripos in ActionScript 3?
indexOf optionally takes the offset within the "haystack" as an argument, and except for the case-insensitive nature of stripos it will do what stripos does. You can make indexOf case-insensitive by using toLowerCase on both the "needle" and the "haystack". Using the variable names as found in the stripos documentation that should give something like:
var position:int = haystack.toLowerCase().indexOf(needle.toLowerCase(),offset);
I'll start with code
function BigScrollUp()
let count = 20
while count > 0
"Press" CTRL-Y <-- how do I emulate this?
sleep 5m
count -= 1
endwhile
endfunction
I want to create a function to quickly scroll up and down, with animation so that I can keep track of where I am going.
You can use feedkeys(). Type :help feedkeys to read more:
feedkeys({string} [, {mode}]) *feedkeys()*
Characters in {string} are queued for processing as if they
come from a mapping or were typed by the user. They are added
to the end of the typeahead buffer, thus if a mapping is still
being executed these characters come after them.
The function does not wait for processing of keys contained in
{string}.
To include special keys into {string}, use double-quotes
and "\..." notation |expr-quote|. For example,
feedkeys("\<CR>") simulates pressing of the <Enter> key. But
feedkeys('\<CR>') pushes 5 characters.
If {mode} is absent, keys are remapped.
{mode} is a String, which can contain these character flags:
'm' Remap keys. This is default.
'n' Do not remap keys.
't' Handle keys as if typed; otherwise they are handled as
if coming from a mapping. This matters for undo,
opening folds, etc.
Return value is always 0.
call feedkeys("\<C-Y>")
Try this:
" Press CTRL-Y:
normal <Ctrl+v><Ctrl+y>
Literally type ctrl+v, followed by ctrl+y which will result in a single character shown as ^Y in your script.