Python Forum
Problem with for loops - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Problem with for loops (/thread-12797.html)



Problem with for loops - erfanakbari1 - Sep-13-2018

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)



RE: Problem with for loops - Larz60+ - Sep-13-2018

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)



RE: Problem with for loops - gruntfutuk - Sep-14-2018

@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.


RE: Problem with for loops - bluefrog - Sep-16-2018

This was a wrong post!!!


RE: Problem with for loops - gruntfutuk - Sep-17-2018

(Sep-16-2018, 09:31 PM)bluefrog Wrote: This was a wrong post!!!

How so?