Home > database >  program that prompts user to enter string until string starts with "A" then prints longest
program that prompts user to enter string until string starts with "A" then prints longest

Time:04-25

Hi im trying to write a program where the user inputs multiple strings until a string begins with the letter "A". which then results in the console printing the longest string that was entered (ie, most characters entered in the string). I am struggling to work out how to achieve this as using letters instead of numbers has gotten the better of me.

The desired output is;

Enter a string: Faces all glow.
Enter a string: Time for summer fun 
Enter a string: As the happy faces show.
Longest string: "As the happy faces show."

find below my code ( note this code isn't 100% accurate, can only get seem to get multiple inputs using the .split() function and there is no limit on how many strings can be entered, the string will end when the condition is met ... meaning the string starts with the letter "A")

a, b, c, d, e, f, g = input("Enter seven values: ").split()
print("Enter a String: ", a)
print("Enter a String: ", b)
print("Enter a String: ", c)
print("Enter a String: ", d)
print("Enter a String: ", e)
print("Enter a String: ", f)
print("Enter a String: ", g)
print()

CodePudding user response:

I must say what you are expecting and your sample code are not relevant at all. However, I think you are asking something like the following code can do

longestString = ""
a = " "
while a[0].capitalize() != 'A':
    a = input("Enter a String: ")
    if len(a) > len(longestString):
        longestString = a
    
print(longestString)

CodePudding user response:

Use a while loop and a flag (boolean variable) to achieve this:

#initialize
stringWithA = False  #a flag, will be set to True if user enters string starting with A
currentMaxLength = 0
currentLongestStr = ''

while stringWithA == False: #haven't seen a string starting with A so far
    x = input("Enter a string: ")
    if x[0] == 'A':
        stringWithA = True
    if len(x) > currentMaxLength:
        currentMaxLength = len(x)
        currentLongestStr = x
print('"'   currentLongestStr   '"')

CodePudding user response:

Your wording seems quite confusing and other comments seem to agree with me, but from what I understand the following code should give you the desired output:

inp = " "
longestString = ""
while inp[0] != 'A':
    inp = input("Enter a String: ")
    if len(inp) > len(longestString):
        longestString = inp
    # To make sure empty input does not break code
    if len(inp) == 0:
        inp =" "
    continue
print("Longest string: ", longestString)

CodePudding user response:

A similar solution but with consideration to lines with the same length that it is the longest length.
inp = " "

longest = list()
length = 0

while inp[0] != 'A':
    inp = input("Enter a String: ")

    if len(inp) > length:
        longest.clear()
        length = len(inp)
        # append will happen in the next if 

    if len(inp) == length:
        longest.append(inp)

    inp  = " " # to avoid empty input

print("Longest :")
for x in longest:
    print(x)

Notes

  • Most of the answers stop getting input after 'A' or 'a' when the question says only 'A'.
  • Related