Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if statement in for loop
#1
This runs a blank. Any suggestions?
story="HOW THE WHALE GOT HIS THROAT IN the sea, once upon a time, O my Best Beloved, there was a Whale, and he ate fishes. He ate the starfish and the garfish, and the crab and the dab, and the plaice and the dace, and the skate and his mate, and the mackereel and the pickereel, and the really truly twirly-whirly eel. All the fishes he could find in all the sea he ate with his mouth--so! Till at last there was only one small fish left in all the sea, and he was a small "
for letter in story:
	letter1=letter
	if letter1.isalpha():
    	print(letter1)
Reply
#2
Your closing python tag is wrong, so it's hard to see what the indentation is. Please edit that.

But the code itself seems reasonable and it runs and has output on my machine. How are you running it? If you put a print("starting") as the first line, are you at least getting that output?
Reply
#3
This code prints out the letters and skips blanks and punctuation and other non-alphas.
story="Hello world!"
for letter in story:
    if letter.isalpha():
        print(letter)
I played around with different indentation and cannot see how this code, or your code, could print a blank/space. Are you sure the code you posted is the code you ran?
Reply
#4
isalpha() will only return True if it is an alpha character a-z or A-Z

>>> ' '.isalpha()
False
>>> '1'.isalpha()
False
>>> 'd'.isalpha()
True
>>> 'D'.isalpha()
True
This part is unnecessary
Quote:
    letter1=letter

so it makes me think about what else is in your code not shown to us in the code snippet provided
Reply
#5
Thank you everyone. I was using the python interpreter at w3schools - are online interpreters unreliable perhaps?
Reply
#6
Not sure. I was able to run the code above on w3schools online python and got the output expected. You might try reloading the page or repasting the code.
Reply
#7
I would be really surprised if isalpha doesn't work. You can test easily enough using the examples in metulburr's post.

You should be trying to debug this problem yourself. The first thing I would have done was start with a much shorter and more interesting story, at least from a programming point of view.
story = 'aA 12 [email protected]#$%^&*()+=-~'
for letter in story:
    if letter.isalpha():
        print(letter, letter.)
If this didn't give me the expected results I would test if isalpha worked as expected.
story = 'aA 12 [email protected]#$%^&*()+=-~'
for letter in story:
    print(letter, 'isalpha =', letter.isalpha())
Depending on the results I would check the documentation to determine if I was doing the loop right or using isalpha() correctly.

After doing that research I might consider posting a question on the forum. My research would probably solve the problem and I wouldn't have to wait for an answer. If I still had questions I would ask a better question and be in a better position to understand the answer.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare each element of an array in a logic statement without using a for loop leocsmith 3 1,028 Apr-01-2021, 07:57 PM
Last Post: deanhystad
  how to create pythonic codes including for loop and if statement? aupres 1 605 Jan-02-2021, 06:10 AM
Last Post: Gribouillis
  Help: list comprehension for loop with double if statement mart79 3 893 May-04-2020, 06:34 AM
Last Post: buran
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 1,478 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619
  Trying to include an If statement in my While Loop junkankit 3 1,326 Jan-31-2019, 10:06 AM
Last Post: buran
  How get attributes of maps from loop statement LB_994 3 1,675 Aug-21-2018, 03:24 PM
Last Post: LB_994

Forum Jump:

User Panel Messages

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