Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

This suddenly happened to both of my codes.

I need help with this i have no idea how to fix i spent 2 hours on it none stop and it wouldn't work.

trys = 1

h = ["warrior","cook","horseman","RoyalGaurd","Hunter","Murderer","Archer","SpearMan"]
print(h)
name = input("type in the number of the word that does not fit in... ")
while name != "2":
    if trys > 3:
        sys.exit("Sorry out")
    trys += 1
    name2 = input("try again... ")   
    if name2 == "2":
        print("correct!")
        del h[1]
        print(h)
        break

else:
    print("correct!")
    del h[1]
    print(h)

I dont want to use break but i had to, because it keeps repeating if i dont. can someone help?

and it says try again like 6 times and i only want to say it 2 times.

thank you so much.

and another note, i had to break it because it will print out sys.exit("sorry out")

thats my 2nd code

import sys

#While loops

PASS = "CrasMeat"
Trys = 2

Password = input("Please enter your Password.. \n ")
while Password != PASS:
    if Trys > 3:
        sys.exit("sorry too many invalid attempts")
    Ra = input("invalid Password please try again.. \n ")
    Trys += 2
    if Ra == PASS:
        print("Welcome Abada")
else:
    print("Welcome Abada")

****The Outcome is for the 2nd attempt, ( thats what Im struggling on on both codes): Please enter your Password..... **For example....crasmeat invalid Password please try again... CrasMeat Welcome Abada sorry too many invalid attempts..

that line above is the one that is annoying me. thank you.

2 Answers

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Jassim,

Let's walk through your program (the second attempt you described).

We'll start the program, so Trys is initially set to 2, and when it first asks us for a password, let's enter python. Thus python gets assigned to the Password variable.

python is not CrasMeat so we enter the while loop.

Trys is less than 3, so we don't trigger sys.exit.

Now we get asked for the password again, so let's now enter CrasMeat. This time, the password we entered gets saved to the Ra variable and not to the Password variable (which is still python).

Trys gets increased by 2, so is now 4.

Ra (CrasMeat) is equal to PASS so we print the welcome message.

Since Password is still python, the loop goes round again.

Trys is 4 which is > 3 so we trigger the "too many invalid attempts" message and trigger sys.exit.

You didn't describe exactly how you wanted or expected your program to behave, and there was no link to a challenge, so I can't tell you how to get from where you are to where you want to be, but hopefully this step by step was clear enough to give you a hint for whatever change you need to make.

Cheers

Alex

Thank you so much Alex! Here is what i changed...

PASS = "CrasMeat"
Trys = 1

Password = input("Please enter your Password.. \n ")
while Password != PASS:
    if Trys > 3:
        sys.exit("sorry too many invalid attempts")
    Password = input("invalid Password please try again.. \n ")
    Trys += 2

else:
    print("Welcome Abada")
Darren Harrison
PLUS
Darren Harrison
Courses Plus Student 5,126 Points

Just a quick look in the first code shows a variable name and name2...these are not the same...the while loop uses name and you introduce name2 later...I think this is why you have to use break. Change name2 to name and you shouldn't have to use break...I think.

Hope this helps! Darren

ohhhh thanks @DarrenHarrison