-1

So I have this program where I need the user to choose a length for a key in python but I want the choice of the user to be restricted with the provided lengths only and I don't quite know how to do that here is the code

available_len = [5 , 10 , 14 , 12 , 15, 16 , 18 , 20 , 21]
length = int(input("[+]:Select the length you want for your key: "))

I want to make so that if the user inputs a length not available in the list to print "Please choose a valid length" and take him back to input the length

I have tried:

 while True:
        length = int(input("[+]:Select the length you want for your key: "))
        if 5 <= length <= 21:
            break
SAM Acc
  • 38
  • 8
  • check the result from input. if it is not among choices, raise an exception. – Abel Jan 03 '22 at 20:28
  • Well, do you know how to repeat code? Do you know how to print the message? Do you know how to check whether the `length` is in the list? What exactly is preventing you from putting these pieces together? If you don't know one or more of those things, they're easy to look up, and have been answered many times before. (Or you could try following a tutorial). Otherwise, you [need a more specific question](https://meta.stackoverflow.com/questions/284236), which starts with trying to write the code. – Karl Knechtel Jan 03 '22 at 20:28
  • How do I make it so it compares them like use and if-else statement or like use % or something like that – SAM Acc Jan 03 '22 at 20:30
  • You want to know whether the `length` is in the `available_len` or not, right? Did you try putting "in python, how do I check whether a value is in a list?" into a search engine? Or anything remotely like that? – Karl Knechtel Jan 03 '22 at 20:31
  • Sounds like a great job for the [walrus](https://realpython.com/python-walrus-operator/). – S3DEV Jan 03 '22 at 20:31
  • yeah it came with different solutions that I don't want – SAM Acc Jan 03 '22 at 20:31
  • 1
    Sorry, but in the real world, you have to make an effort to look for solutions; you have to think about the results that you find; and [when all else fails](https://meta.stackoverflow.com/questions/261592), you need to make a clear effort to communicate about why and how you are still stuck. – Karl Knechtel Jan 03 '22 at 20:33
  • @KarlKnechtel I have tried something if this is what is bothering you I will apply to the code – SAM Acc Jan 03 '22 at 20:33
  • @KarlKnechtel it's okay man you are free to do whatever I was just trying to fix something that got you mad – SAM Acc Jan 03 '22 at 20:40
  • I am not angry, or otherwise emotional in the slightest. I am only trying to explain how the site works. I gave you multiple helpful links already. – Karl Knechtel Jan 03 '22 at 20:42
  • Yes I did not assume that you were not helpful and I did see the reason for your downvote so I fixed the mistake I made by not showing what I have done so yeah that's it really – SAM Acc Jan 03 '22 at 20:44

3 Answers3

1

Try:

available_len = [5 , 10 , 14 , 12 , 15, 16 , 18 , 20 , 21]
length = -1
while length not in available_len:
    length = int(input("[+]:Select the length you want for your key: "))

Output:

[+]:Select the length you want for your key: 7
[+]:Select the length you want for your key: 30
[+]:Select the length you want for your key: 14
Corralien
  • 109,409
  • 8
  • 28
  • 52
1

Simply loop until a wanted answer is received:

available_len = [5 , 10 , 14 , 12 , 15, 16 , 18 , 20 , 21]
length = -1
while length not in available_len:
    length = int(input("[+]:Select the length you want for your key: "))
    if length not in available_len:
        print("Length not accepted, try again")
print(length)
Eli Harold
  • 2,280
  • 1
  • 3
  • 22
1

You can't control what the user inputs. You can control whether or not you ask for a different input.

Model this with an infinite loop and an explicit break statement.

available_len = [5 , 10 , 14 , 12 , 15, 16 , 18 , 20 , 21]
while True:
    length = int(input("[+]:Select the length you want for your key: "))
    if length in availble_len:
        break

While you can shorten this using an assignment expression, I find this approach to be clearer.

available_len = [5 , 10 , 14 , 12 , 15, 16 , 18 , 20 , 21]
while (length := int(input("[+]:Select the length you want for your key: "))) not in available_len:
    pass

You could replace pass with a print statement explaining why the previous choice was invalid.

chepner
  • 497,756
  • 71
  • 530
  • 681