Home > Software design >  How do I get my program to read the length of a word?
How do I get my program to read the length of a word?

Time:10-07

#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.

CodePudding user response:

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")

CodePudding user response:

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')

CodePudding user response:

Change this! if 6 > len(passw):

  • Related