Python Forum
Complex Jumping In Code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Complex Jumping In Code
#1
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:
        continue
This 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
Reply
#2
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
>>>
Reply
#3
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

Reply
#4
(Mar-15-2020, 08:16 AM)buran Wrote: 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.

Yeah Just A Matter Of Habit
Reply
#5
(Mar-15-2020, 09:58 AM)WhamDie Wrote: Yeah Just A Matter Of Habit
Yeah, 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

Reply
#6
What's with the weird capitalisation of every word in your posts? It does make them more difficult to read.
Reply
#7
(Mar-15-2020, 08:06 AM)Larz60+ Wrote: 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
>>>
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 Habit
Yeah, 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 Big Grin Big Grin

Anyway, Can You Help Me With The Code?
Reply
#8
Please stop writing like that and just write normally.
Reply
#9
(Mar-15-2020, 10:43 AM)WhamDie Wrote: It Became A Habit
It 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

Reply
#10
(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 Help
To 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_lst
Test.
Output:
>>> word_list = words() How many words do you want to enter: 2 Enter a word: 99 Use alphabets only,try again Enter a word: *.. Use alphabets only,try again Enter a word: hello world Enter a word not a sentence,try again Enter a word: hello Enter a word: world >>> word_list ['hello', 'world']
Reply


Forum Jump:

User Panel Messages

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