I am completely new to programming and python so i wanted to know if there is any way to for the program to recognize when i have pressed a specific key and continue rather than using input() function where i have to press enter after i have entered my data.
I've tried using the getch() but the program just continues without waiting for a key to be pressed.
from msvcrt import getch
x=0
while ord(getch() == 117):
print("worked")
TypeError: ord() expected string of length 1, but bool found
You wrote this expression:
ord(getch() == 117)
Instead, use one of these (equivalent) expressions:
ord(getch()) == 117
getch() == chr(117)
getch() == 'u'
Below is a shortened version of my code, without all the validation. I am writing a program that tells the user how strong their password is, by seeing their overall score at the end. If the password has 3 letters next to each other in a row, and those three letters are also next to each other on the 'qwerty' keyboard, then their overall score goes down by 5. I have created a dictionary to assign each letter on the keyboard a value, and then if 2 consecutive letters in the password have a difference of 1, it means there are 3 letters in a row on the keyboard.
However, I keep getting a
ValueError: invalid literal for int() with base 10:
I don't really know how to use dictionaries, so any help is much appreciated!
password=str(input("Please enter a password with more than 4 digits, and it should only be letters:"))
score=0
keyboard={'Q':1,'q':1,'W':2,'w':2,'E':3,'e':3,'R':4,'r':4,'T':5,'t':5,'Y':6,'y':6,'U':7,'u':7,'I':8,'i':8,'O':9,'o':9,'P':10,'p':10,'A':12,'a':12,'S':13,'s':13,'D':14,'d':14,'F':15,'f':15,'G':16,'g':16,'H':17,'h':17,'J':18,'j':18,'K':19,'k':19,'L':20,'l':20,'Z':22,'z':22,'X':23,'x':23,'C':24,'c':24,'V':25,'v':25,'B':26,'b':26,'N':27,'n':27,'M':28,'m':28}
for n in range ((len(password))-2):
if (int(password[n+1])-int(password[n])==1) and (int(password[n+2])-int(password[n+1]==1)):
score=score-5
print(score)
If your password input is only letters, then this following line will raise an error.
int(password[n+1])
and so will int(password[n]) and all your other int casts. The reason for this is because you're casting non-digit characters to int. That's what's causing the error you're seeing.
I believe, your intention is to do
int(keyboard[password[n+1]]) - int(keyboard[password[n]]) == 1
but since, the values of your keyboard dictionary are already int's, then the int casts in your if-statement are not required.
keyboard[password[n+1]] - keyboard[password[n]] == 1
I am making a two-player hangman game, and I have set a limit of 10 characters which is working fine:
on keyDown
if the length of me = 10 then
beep
else
pass keyDown
end if
end keyDown
However, when I try to limit the character input to only lowercase characters, nothing seems to happen. I can still type any character I want.
on keyDown inkey
if inkey is in "abcdefghijklmnopqrstuvwxyz" then
pass keyDown
else
beep
end if
end keyDown
How do I fix this? Thankyou.
Don't pass the keyDown message. Instead, put the value of the parameter after the field.
on keyDown theKey
if len(the text of me) >= 10 then
beep
else
put toLower(theKey) after me
end if
end keyDown
The problem occurs, because LiveCode considers lower case and upper case characters as equal:
put ("a"="A")
returns true. Another way to solve this is to set the caseSensitive to true. If you add this line
set the caseSensitive to true
to the beginning of your script, it will do exactly what you want.
I have been doing some homework. The task was to implement System.Int32.TryParse to check if an entered value is a number or something random. I was wondering if there is a built-in method that checks if something entered is a letter and NOT a number. I tried searching google and MSDN in the string type, but with no luck so far. I did write my own implementation, but I was curious.
Tnx
The easiest way to check whether a character is a number is probably to check whether it is in range from '0' to '9'. This works because of the way characters are encoded - the digits are encoded as a sub-range of the char values.
let str = "123"
let firstIsNumber =
str.[0] >= '0' && str.[0] <= '9'
This gives you a bit different behavior than Char.IsDigit because Char.IsDigit also returns true for thigns that are digits in other alphabets, say ႐႑႒႓႔႕႖႗႘႙ I suspect you do not plan to parse those :-).
I was going through problems on SPOJ, when I saw this SBStr1. I learnt a little bit of WhiteSpace language, but I could reach only up to loops.
Can anyone please help me on how to check if a string has another string as a substring in WhiteSpace ?
I'm not going to write the Whitespace code for you but here is an approach you can take that easily translates to Whitespace:
24 times:
read 10 bit number into A
skip space
read 5 bit number into B
skip newline
if (A>>0)%32 == B or (A>>1)%32 == B or ... or (A>>5)%32:
print 1
else:
print 0
print newline
You can implement the bitshifts through repeated division by 2.