How do I get my program to read the length of a word?

Question:

#Task 10.3
passw = tuple()
#empty tuple


passw = input("Create a password: ")
#adds a password into the passw tuple

if passw < str(len(6)):
    print("Your password is too short")
else:
    print("Your password has been changed")

Should be a simple piece of code. Logically, I think it should look like this, but I cannot seem to figure it out.

Asked By: almond6

||

Answers:

You need to apply the len function to passw:

if len(passw) < 6:
    print("Your password is too short")
else:
    print("Your password has been changed")

It’s also worth noting that passw = input("Create a password: ") does not in fact add the password "into the passw tuple" – it just overwrites the tuple with the string value produced by input().

You might want to use getpass instead of input:

#Task 10.3
from getpass import getpass

# reads password from user into the passw variable
passw = getpass("Create a password: ")

if len(passw) < 6:
    print("Your password is too short")
else:
    print("Your password has been changed")
Answered By: Mathias R. Jessen

Change this! if 6 > len(passw):

Answered By: Bob8751

The return value from input() is a string. You can determine the length of the string with the len() function

passw = input('Create a password: ')

if len(passw) >= 6:
    print('Your password has been changed')
else:
    print('Your password is too short')
Answered By: OldBill
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.