-6

Write a program that asks the user to enter a password (with the prompt "Password:"). It should then reward the user with "Congratulations, you're in!" if the password is between 8 and 20 characters long (inclusive), ends with a digit, and contains a period or comma. Otherwise, it should say "Wrong! This incident will be reported!"

I need to be able to have a number at the end and contain either a period or a comma. I am not sure on how to isolate these two parts of the password. What do I do to have the user asked to enter a password?

user_pass = str(input())

if (len(user_pass <= 8) and (len(user_pass >= 20)) and  
    print ("Congratulations, you're in!")
else: 
    print ('Wrong! This incident will be reported!')
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55

2 Answers2

0

Just add more conditions using and.

Python 3:

password = input("Password: ")

if (8 <= len(password) <= 20) and password[-1].isdecimal() and any(x in password for x in {'.', ','}):
    print("Congratulations, you're in!")
else:
    print("Wrong! This incident will be reported!")

Python 2:

password = raw_input("Password: ")

if (8 <= len(password) <= 20) and unicode(password[-1]).isdecimal() and any(x in password for x in {'.', ','}):
    print("Congratulations, you're in!")
else:
    print("Wrong! This incident will be reported!")
Olvin Roght
  • 7,677
  • 2
  • 16
  • 35
  • I tried this code but it gave me this error and I'm not sure how to fix it, also why give this post down votes? I just opened an account on this as a fresh CS major so I am still learning. Expected output ends with Password: <_| Congratulations, you're in! – safarihunter Sep 23 '19 at 20:50
  • Its on zybooks so its not letting me copy and paste it on here like I see it but its telling me that I need to have Password: and an enter right after, then it displays congratulations your in like it is supposed to. – safarihunter Sep 23 '19 at 20:56
  • @safarihunter, probably you're using python 2, I've added code for this version – Olvin Roght Sep 23 '19 at 21:07
  • @safarihunter, it'll be good if you'll [accept](https://stackoverflow.com/help/someone-answers) answer you find best. – Olvin Roght Oct 15 '19 at 13:14
-1

A hint:

def password_is_good(password):
  return (
    password_length_is_correct(password) and
    password_ends_with_digit(password) and
    password_contains_punctuation(password)
  )

def password_length_is_correct(password):
  # implement this, and the rest.

password = str(input())
if password_is_good(password):
  # do something.
else:
  # do something else.

You can access string elements by index, with negative indexes counting from the end; e.g. password[-1] would access the last character.

You can use the in operator to check for a character to be present in a string, like if 'c' in 'abcdef'.

9000
  • 39,899
  • 9
  • 66
  • 104