Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create a loop if contents of a list is in a variable?

I’m asking the user for a name, and if that name contains a number I want to print that they need to remove the numbers. But I can’t exit the loop afterwards.

    numbers=['1','2','3','4','5','6','7','8','9','0']
    vad2=input("To what site are you going to use the password? ")
    for f in numbers:
        if f in vad2:
            while True:
                print("You can't include numbers in the name of the site.")
                vad2=input("To what site are you going to use the password? ")
                numbers=['1','2','3','4','5','6','7','8','9','0']
                for f in numbers:
                    if f in vad2
                        print()
                    else:
                        break;

The first time I use:

for f in numbers:
   if f in vad2:

It works (the code skips the loop if I don’t include any numbers) but when I do the same thing in the loop it wont work for some reason.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Break will jump over your ‘for loop’, not over your ‘while loop’.
Modify your code like

if not vad2.isalpha():   # if you want only alphabet from your input
    print()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading