Python Forum
Print the longest str from user input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print the longest str from user input
#1
Essentially, the user inputs random strings and i am trying to reprint the longest string that the user input (so far it only prints the first non-capital letter). A bit lost on how to fix this.

loopEnd = ""

n = (input("Input: "))
longString = max(n)

while n != loopEnd:
    n = str(input("Input: "))
    if n == loopEnd:
        print("Longest input was", "'",longString,"'")
Reply
#2
you only need one input statement.

In your while loop, append each input to a string
include a keyword to terminate input , like 'quit'
after exiting the input loop, check for and print longest entry in list
Reply
#3
Sorry i am very new and very confused. What would that actually look like in my script?
Reply
#4
Some observations about your code:

- input is string, so no need to use str() for converting
- no need to define n before while loop. Use instead 'while True' and break
- use Larz60+ advice - append user answers to list and print out longest

As this is homework I provide 3.8-only compatible solution which probably will not pass automagic check but gives general idea:

values = []                                             # list to collect user entered strings
while (answer := input('Enter input: ')) != '':         # while loop which assigns user input to answer and checks equality to ''
    values.append(answer)                               # appends answers to values list

print(f'Longest input was {max(values, key=len)}')      # prints out longest element in values list
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
Thanks a bunch. I'm assuming that i need an initial input to declare answer? (my IDE otherwise states its undeclared. Furthermore apparently line 2 a : is expected even though its present?
Reply
#6
As stated - this is only Python 3.8 compatible code (using walrus operator).

Task can be described: 'collect user input until break and find longest input'

So


# assign empty list to a name where user input will be collected

# while True loop - executes until 'break' encountered in the body of loop
# assign name to user input
# check if user input is break symbol or word, if so - break loop
# append answer to list of collected inputs

# print max value by length in list
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Simulate an answer based on user input [Beginner needs guidance] Bombardini 1 1,257 Nov-12-2022, 03:47 AM
Last Post: deanhystad
Sad Find Longest streak for habits johnconar 3 1,822 Sep-12-2022, 07:13 PM
Last Post: deanhystad
Bug How to print only vowels from an input? PP9044 8 7,427 Feb-26-2021, 04:02 PM
Last Post: Serafim
  Print user input into triangle djtjhokie 1 2,343 Nov-07-2020, 07:01 PM
Last Post: buran
  Changing Directory based on user input paulmerton4pope 13 7,885 Aug-14-2020, 11:48 AM
Last Post: GOTO10
  how to add the user input from file into list wilson20 8 4,229 May-03-2020, 10:52 PM
Last Post: Larz60+
  How to print the docstring(documentation string) of the input function ? Kishore_Bill 1 3,499 Feb-27-2020, 09:22 AM
Last Post: buran
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,759 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  how to add user input to a dictionary to a graph KINGLEBRON 3 2,980 Jul-31-2019, 09:09 PM
Last Post: SheeppOSU
  New to Python - tiny coding assistance on user input function and assign to variable Mountain_Duck 1 2,465 Mar-23-2019, 06:54 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020