Python Forum
Problem with for loops
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with for loops
#2
Commeted step-by-step:
# This creates a comma delimitered string
number = "9,234,456,123,456,789"

# An empty string variable
cleanedNumber = ''

# this looks at number, character by character, (9, then , then 2 ...)
for char in number:

    # checks if char in (is part of) string '0123456789. (0, 1, 2 ... or 9), (could have used isdigit)
    if char in '0123456789':

        # This sets the appends the value of char to the current value of cleaned Number
        cleanedNumber = cleanedNumber + char
 
        # this sets newNumber to the integer value of cleanedNumber 
        newNumber = int(cleanedNumber)

        # This displays value of newNUmber
        print("the Number is {}".format(newNumber))
 
        # This sets state to each of the four strings, one each iteration
        for state in {"not pinin'","no more", "a stiff", "bereft or life"}:

            # Display the value of state.
            print("This parrot is "+ state)
Reply


Messages In This Thread
Problem with for loops - by erfanakbari1 - Sep-13-2018, 10:01 AM
RE: Problem with for loops - by Larz60+ - Sep-13-2018, 10:20 AM
RE: Problem with for loops - by gruntfutuk - Sep-14-2018, 04:57 PM
RE: Problem with for loops - by bluefrog - Sep-16-2018, 09:31 PM
RE: Problem with for loops - by gruntfutuk - Sep-17-2018, 02:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with nested loops robert5502001 7 3,663 Aug-01-2022, 06:26 AM
Last Post: stevensanders

Forum Jump:

User Panel Messages

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