w=[] r=0 t=0 q=int(input("How Many Numbers You Wanna Enter: ")) for i in range(q): e=input("Enter A Word: ") if e.isalpha==True: for i in e: if i==" ": print("Enter A WORD Not A SENTENCE") continue else: w.append(e) else: print("Use Alphabets Only") continue for i in w: r=len(i) if t<r: t=r else: continue for i in w: if len(i)==t: print("Longest Word Is: ", i) else: continueThis Code Is For Printing The Longest Word In A List Of Words. Instead Of Using continue, I Wanna Do Something Do Repeat The Iteration, Like When The User Enter A String With Space Or A Digit, The User Is Prompted Again To Enter The Same Value. Using break Will Stop The Whole Program, Which I Definitely Not Wanna Do. What Should I Do? Please Help
Complex Jumping In Code
Complex Jumping In Code
|
why not:
>>> words = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum" >>> wordlist = words.split() >>> print(f"The longest word in the list is {max(map(len, wordlist))} characters") The longest word in the list is 13 characters >>>
A bit of advise - get rid of single char variable names. The sooner the better. Use meaningful names. It's a nightmare to understand/follow and maintain such code.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs
Mar-15-2020, 09:58 AM
(Mar-15-2020, 09:58 AM)WhamDie Wrote: Yeah Just A Matter Of HabitYeah, that's exactly what I mean - get rid of that habbit. It will help you both in short-term (more people will look at your code and likely to help) and long-term - you will have maintainable code and be better programmer.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs
Mar-15-2020, 10:36 AM
What's with the weird capitalisation of every word in your posts? It does make them more difficult to read.
(Mar-15-2020, 08:06 AM)Larz60+ Wrote: why not:What I'm Trying To Achieve Is Jumping And Input Methods And Not Finding Longest Word. (Mar-15-2020, 10:36 AM)ndc85430 Wrote: What's with the weird capitalisation of every word in your posts? It does make them more difficult to read.Ok (Mar-15-2020, 10:04 AM)buran Wrote:(Mar-15-2020, 09:58 AM)WhamDie Wrote: Yeah Just A Matter Of HabitYeah, that's exactly what I mean - get rid of that habbit. It will help you both in short-term (more people will look at your code and likely to help) and long-term - you will have maintainable code and be better programmer. It Became A Habit When I Used To Make Mods For Games 3 Years Ago And Other Modders Would Decompile My Scripts And Steal Codes. This Is A Homework Or Else I'd Have Written It In Such A Manner It'd Take You Years To Understand ![]() ![]() Anyway, Can You Help Me With The Code?
Mar-15-2020, 06:59 PM
Please stop writing like that and just write normally.
(Mar-15-2020, 10:43 AM)WhamDie Wrote: It Became A HabitIt looks like you have a lot of habits to get rid of... And as it's homework I am moving it to respective section...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs (Mar-15-2020, 07:52 AM)WhamDie Wrote: I Wanna Do Something Do Repeat The Iteration, Like When The User Enter A String With Space Or A Digit, The User Is Prompted Again To Enter The Same Value. Using break Will Stop The Whole Program, Which I Definitely Not Wanna Do. What Should I Do? Please HelpTo show one way,all code in global namespace make it hard to do logic as repeat and ask question again. So here a function for the first part, return will break all as it goes our of function.When this part is finish it will not mess with rest of code,as the point with a function is that code inside is isolated. def words(): word_lst = [] word_count = int(input("How many words do you want to enter: ")) while True: word = input("Enter a word: ") if any(c.isspace() for c in word): print("Enter a word not a sentence,try again") elif not word.isalpha(): print("Use alphabets only,try again") else: word_lst.append(word) if len(word_lst) >= word_count: return word_lstTest.
|
|
Users browsing this thread: 1 Guest(s)