0

I made this piec of code and it works just fine for the following:

Write the code to check whether a string given through input()contains only valid characters, but stop once you reach one invalid character. So if the string is valid print valid string. If the string is invalid print invalid string till that point.

But now i have to use a while loop for this and i can't get it done, can you guys help me or explain to me how convert this code to a while loop, while maintaining the exact same function.

file = input()

invalid_letter_found = False
correct_letters = []

for current_letter in file:
    if current_letter in ['A', 'T', 'G', 'C']:
        correct_letters.append(current_letter)
        continue
    elif current_letter != ['A', 'T', 'G', 'C']:
        invalid_letter_found = True
        break
if invalid_letter_found == True:
    print(f'invalid {"".join(correct_letters)}')
else:
    print(f'valid {"".join(correct_letters)}')
2
  • Python's 'for' and 'while' statements are both used to construct loops. The 'for' statement iterates over items of any sequence (a list or a string), in the order that they appear in the sequence. The 'while' statement typically involves one or more variables that are initialized either at the start of or prior to entering the loop and then updated somewhere within the loop. Commented Sep 30, 2020 at 13:35
  • To resolve your question, 1. Identify a means to iterate over your list 2. Look at the construct of a while True loop and how best to end the loop Commented Sep 30, 2020 at 13:36

2 Answers 2

1

This program will give the exact same output as above program in the query

file = input()
n=len(file)
lst=[]
for alp in file:  # this will convert the string to list, you can use any other method if you don't want for loop
    lst.append(alp) #Here the conversion ends
invalid_letter_found = False
correct_letters = []
i= 0
while i < n:
    if lst[i] in ["A", "T", "G", "C"]:
        correct_letters.append(lst[i])
        i += 1
        continue
    elif lst[i] != ["A", "T", "G", "C"]:
        invalid_letter_found = True
        break
if invalid_letter_found == True:
    print(f'invalid {"".join(correct_letters)}')
else:
    print(f'valid {"".join(correct_letters)}')
Sign up to request clarification or add additional context in comments.

Comments

0

Although the only reason you would NEED to use a while loop is if this is a homework question - here's a nudge in the right direction:

  1. You could progressively remove letters from the string using string slicing
current_letter = file[0]
file = file[1:]
  1. You could use the string length as your condition for your while loop - once the string length is zero, you are done:
while len(file):

3 Comments

Thanks for the advice now i understand how i could make it work and yes you are right it is indeed a homework question....
Unless you are decreasing the size of file as part of your while loop operation, this exit criteria won't work
@itprorh66 That's true, which is why I included the string slicing operations current_letter = file[0] to get the first letter, and file = file[1:] to reduce the string size by one. Based on the original code, I think it's fairly obvious that these go inside the while loop since that's where you're using current_letter... but it's a fair point nonetheless.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.