I'm just learning Python and I'm putting into practice what I've learned so far by making a login system. I want to use a password of at least 6 characters and one letter, and here comes my first question: How can I set that limit so that when I enter more than 6 characters, a message appears print
saying that the minimum is 6? On the other hand, in the code that I show below I have another problem, when entering a text or letter input
, the error appears:
Traceback (most recent call last): File "codigo.py", line 30, in <module> prueba2() File "codigo.py", line 6, in prueba2 Request_Password=int(input("Insertar contraseña")) ValueError: invalid literal for int() with base 10: 'PruebaContraseña'.
My code is the following:
contraseñas=[123]
Archivos=["Documentos","Videos"]
def prueba2():
Access=False
Request_Password=int(input("Insertar contraseña"))
if Request_Password in contraseñas[:]:
Access=True
elif Access==True:
seleccion()
while Access==False:
prueba2()
break
def seleccion():
confirmacion=input("¿Que archivo desea ver?")
if confirmacion in Archivos[:]:
print("Acceso autorizado a",confirmacion)
else:
print("Esa categoria no existe")
prueba2()
It's just the beginning, but I can't continue without knowing how to do what I've already told you. Everything works fine when only when I use either number or letters but not both combined.
The problem you're having is because you're trying to int the user input, this because of the 'int' at the beginning in:
and since you are testing with "TestPassword" it throws you the error:
What you can test is only with the input and then check the conditions (length greater than 6 and containing a letter). Surely this answer guides you much better:
1) To make my explanation shorter, I will first answer the question of the error:
If you look at the error message it gives you a hint of what is happening
ValueError: invalid literal for int() with base 10: 'PruebaContraseña'
.This error occurs because you are trying to convert to an integer something that python does not recognize as a number. For example, if you enter the password value "123a" and apply the function
int
, python won't really know how to convert that to an integer because it's not clear what number it represents.So as your requirement is to use a password that accepts alphanumeric values, it is recommended to use a string (text string). Then, as the
Request_Password
default variable receives a text string, it is not necessary to useint
, and in the passwords variable you could have something like this:contraseñas=["123"]
so that they are the same data type and be able to carry out the comparison.2) Now for the question to limit the number of characters simply use the function
len
to find the length of a text string and a conditional to ask if it is greater than 6, for example:Although you have already answered the two specific questions, I am going to make some more comments on the code. The error is simply because you try to cast
int
what was entered viainput
as you have been told:when you are entering a string with non-numeric characters. That casting is simply unnecessary,
input
it returns a "raw" string and your password must be the same. The list of passwords on the other hand must contain strings, not integers since you yourself define that you want the password to have "at least one letter"That said, a few observations:
You don't need list slicing
[:]
to check if the item is in the list, it's not necessary and it's inefficient as it creates a shadow copy of the list .Do not compare boolean values with
==
. When you want to check if an object evaluates to true (True
) or false (False
) don't useif varible == True
, this is considered a "design anti-pattern" just useif variable:
orif not variable:
.The function in charge of capturing the password has several problems, first
if
you check if the password is correct and if it is, you makeAccess
itTrue
, if this happenselif
it is not executed. The clauseelif
is not evaluated at the same time that theif
previous one is evaluated as true (that is precisely what differentiates it fromif
chained). The functionseleccion
is therefore never called.On the other hand, the loop
while
is meaningless since you break it on the first iteration with thebreak
, a conditional would suffice. In any case, consider eliminating the recursive call and include all the logic of the function in the loop, as a general rule iteration is more readable and efficient than recursion.To find out the number of characters or items in any iterable, simply use the built-in function
len()
. To validate that the password has at least one letter, you can use the methodstr.isalpha
that returnsTrue
if the string only contains only alphabetic characters applied on each character of the string. You can iterate through the string with afor
, but it's better to useany
one that returnsTrue
if at least one of the items evaluates to true.As there are almost always a thousand ways to do the same thing, one of them can be: