Python Forum
Problem with for loops
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with for loops
#1
Hey guys , I have started python developer programming course since 1 week ago and now i'm facing a problem with advanced for loops section I was wondering maybe you can help me in this .
The problem is I can't understand the syntax of this and can't figure it out what this code is supposed to run for us actually !
number = "9,234,456,123,456,789"
cleanedNumber = ''

for char in number:
	if char in '0123456789':
		cleanedNumber = cleanedNumber + char

		newNumber = int(cleanedNumber)
		print("the Number is {}".format(newNumber))

		for state in {"not pinin'","no more", "a stiff", "bereft or life"}:
			print("This parrot is "+ state)
Reply
#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
#3
@erfanakbari1, when I started learning Python, I found the Python Visualizer very helpful as it allows you to step, visually, through the execution of a programme and see what is going on.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#4
This was a wrong post!!!
Reply
#5
(Sep-16-2018, 09:31 PM)bluefrog Wrote: This was a wrong post!!!

How so?
I am trying to help you, really, even if it doesn't always seem that way
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with nested loops robert5502001 7 3,570 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