How do I get the values in my dictionary that I have created in python, to be used, instead of the strings?
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)
python-3.x dictionary valueerror
add a comment |
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)
python-3.x dictionary valueerror
Did you mean to check your dict for the values? Right now you're casting your characters to directly to int in line 5 without mapping them through your dict to a number which is giving you the error you're seeing.
– nathan.medz
Nov 22 '18 at 0:52
add a comment |
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)
python-3.x dictionary valueerror
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)
python-3.x dictionary valueerror
python-3.x dictionary valueerror
asked Nov 22 '18 at 0:36
Atara KleinAtara Klein
84
84
Did you mean to check your dict for the values? Right now you're casting your characters to directly to int in line 5 without mapping them through your dict to a number which is giving you the error you're seeing.
– nathan.medz
Nov 22 '18 at 0:52
add a comment |
Did you mean to check your dict for the values? Right now you're casting your characters to directly to int in line 5 without mapping them through your dict to a number which is giving you the error you're seeing.
– nathan.medz
Nov 22 '18 at 0:52
Did you mean to check your dict for the values? Right now you're casting your characters to directly to int in line 5 without mapping them through your dict to a number which is giving you the error you're seeing.
– nathan.medz
Nov 22 '18 at 0:52
Did you mean to check your dict for the values? Right now you're casting your characters to directly to int in line 5 without mapping them through your dict to a number which is giving you the error you're seeing.
– nathan.medz
Nov 22 '18 at 0:52
add a comment |
1 Answer
1
active
oldest
votes
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
Thank you @ TrebuchetMS ! this solved my problem!
– Atara Klein
Nov 22 '18 at 17:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422367%2fhow-do-i-get-the-values-in-my-dictionary-that-i-have-created-in-python-to-be-us%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
Thank you @ TrebuchetMS ! this solved my problem!
– Atara Klein
Nov 22 '18 at 17:35
add a comment |
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
Thank you @ TrebuchetMS ! this solved my problem!
– Atara Klein
Nov 22 '18 at 17:35
add a comment |
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
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
edited Nov 22 '18 at 1:01
answered Nov 22 '18 at 0:52
TrebledJTrebledJ
3,33011228
3,33011228
Thank you @ TrebuchetMS ! this solved my problem!
– Atara Klein
Nov 22 '18 at 17:35
add a comment |
Thank you @ TrebuchetMS ! this solved my problem!
– Atara Klein
Nov 22 '18 at 17:35
Thank you @ TrebuchetMS ! this solved my problem!
– Atara Klein
Nov 22 '18 at 17:35
Thank you @ TrebuchetMS ! this solved my problem!
– Atara Klein
Nov 22 '18 at 17:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422367%2fhow-do-i-get-the-values-in-my-dictionary-that-i-have-created-in-python-to-be-us%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Did you mean to check your dict for the values? Right now you're casting your characters to directly to int in line 5 without mapping them through your dict to a number which is giving you the error you're seeing.
– nathan.medz
Nov 22 '18 at 0:52