I want to create a while-loop that loops until a random string has been created that starts with "A" and ends with "Z".
import string
from random import choice
def random_string():
""" Generates a 5 letter random string. """
rs = ''.join(choice(string.ascii_uppercase) for _ in range(5))
print(rs)
while random_string()[-1] != "Z" and random_string()[0]) != "A":
print("keep going")
return random_string
So far I have this and I am running into trouble with the while loop checking for the first and last letter. Is there a simpler (and correct) way to test this?
Thanks in advance.
whilecondition.whilecondition were wrong, and the operator to check logical AND in Python isand, not&return rsbefore you ever enter the loop.