0
while True:
    name = input('Enter Texture Pack Name- ')
    name_length = len(name)

    if len(name) <= 0:
        print ('You didnt write anything, try again.')

    if len(name) == 1:
        print ('Thats not enough, try again.')

    if len(name) >= 18:
        print ('Thats to much, try again.')    

    if int(name_length) >= 2:
        break

print (('Texture Pack- %s') % (name))

So I'm trying to make 2 limits for the names description, it being too small and it being too large. I set the break to >= 2 so anything above the 18 max limit will break. How would I tell it to break only if the length is more than 2 but less than 18?

2 Answers 2

2

First of all, there's a couple issues that I see.

Not all the code is in your while loop. All of the if statements should be in your while loop simply indent this. This is probably what it should look like:

while True:
    name = input("Enter Texture Pack Name- ")
    name_length = len(name)

    if name_length <= 0 or >=18:
        print("There's an error!")
        break
    else:
        print("texture pack- %s" % (name)

Hopefully that helps. Just so you know, the break must be within a loop.

Edit: Discard the top. I'm rewriting this!

while True:
    try:
        name = input("Enter texture pack name: ")
        name_length = int(len(name))
    if name_length <= 0:
        raise ValueError("Nothing was written!")
    elif name_length == 1:
        raise ValueError("Only one character!")
    elif name_length >= 18:
        raise ValueError("More than 18!")
    else:
        break
print ("Texture pack: %s" % (name))

This will keep asking for the right input until it get's it right.

Sign up to request clarification or add additional context in comments.

3 Comments

Thats was just me not indenting it correctly in the example sorry about that.
Ok! That got me really confused! I'll change up my answer now with that.
This actually ended up working better for me thanks for showing me "ValueError"
2

Try this:

name = input('Enter Texture Pack Name- ')
while len(name) <= 2 or len(name) >= 18:
    print('Error - texture pack name length should be between 2 and 17 characters')
    name = input('Enter Texture Pack Name- ')

6 Comments

Yeah, that has to be there otherwise the len(name) function call will complain that name is not defined. Setting it to '' also ensures enters the loop on the first evaluation of the expression.
I see an issue with that; aren't you trying to calculate the length of the texture pack name with len? In this case, the while loop will be executed even if the length is <= 2 or >=18.
@Zizouz212 But that is when we want the user to enter a different texture pack name, when that condition holds.
Then you should be using a try except block then, and raise a ValueError when something like that comes up.
the error should say "Error - texture pack name length should be between 2 and 17 characters" would have changed it myself but my rep sucksXD
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.