It is currently 30 Oct 2020, 15:35

• Featuring Members' Works (Image Attachments) •
1: member
2: member
3: member
4: member
5: member

GIMP/GL:  Tutorials | Tips | Questions | Interests | Script Requests
Recommendations:  Index | Top 10
Resources:  Python-Fu Plug-ins | Scheme-Fu Scripts | GIMP
Python:  Lessons | Questions | 🡆 Exercises
The Arts:  GIMPed | GIMPed Photos | Other | Contests | Public Domain Images | Isolated Images | Blender
Inkscape
Social:  Say Hello | Anything Goes | Shares | FCSP

Input and For loops Exercise

Post Reply
tim
GL Blogger

Input and For loops Exercise

Post#1 by tim » 17 Nov 2018, 18:22


Write a program that
1. Prompts the user for input of a string
2. Uses for loop to loop through each character of string and inserting each character to the beginning of a list which starts out as empty list (Note: a string can be viewed as a list of characters/letters).
3. Uses for loop to loop through each character of the inserted list and prints out each character/letter so that the input string from step 1. is now print out in reverse. (Note: to print multiple print statements without newline character between them, just pass in a 2nd named parameter of end="" which tells it to not end a line with newline character).

Solution:
Spoiler:

Code: Select all

s = input("Enter a string:")

l = []
#loop through each letter in string s and 
#keep inserting to beginning of list l
for letter in s:
    l.insert(0,letter)
    
#loop through each letter in l and printing it out
for letter in l:
    print (letter,end="")
aka Tin

         

Krikor
Forum Member

Post#2 by Krikor » 20 Nov 2018, 04:28


Tim,
I could not use For Loop. :hammer_head:
I had trouble understanding what the problem was asking for.
Besides understanding the logic of the exercise, I also had to understand the text written in English. :oops:

I thought I would at some point apply the

Code: Select all

mylist.insert (index, item)
and add the characters in the empty list.
But I didn't know how to get the characters individually from the string provided by the user. :?

And with the example of the For Loops lesson, I was not able to apply this to a list.
For item in Mylist []: ??

Looking for For Loops, I found an example of how to use While loop, and I ended up understanding this function better.

Temporarily resolved (I think) using While.
But I still will not check the answer. I will continue trying to understand the For Loop, and then try again to solve this exercise.
For now this is my solution:
Spoiler:

Code: Select all

k=input("Enter your name"); print("")
x=(len(k)-1)
while x <= len(k) and x>=0: 
   print(k[x], end="")
   x-=1
Tim, thanks for the fun !! :thank_you:
"I feel that in both art and music, it's not the success that matters but the pleasure it gives you. Focus on the pleasure and the learning will come naturally." - Brian Weston

tim
GL Blogger

Post#3 by tim » 20 Nov 2018, 06:27


As long as you're able to come up with solution then I guess you've learned something.
While loops are fine as an alternative.
aka Tin

         

Krikor
Forum Member

Post#4 by Krikor » 20 Nov 2018, 19:37


tim wrote:
20 Nov 2018, 06:27
As long as you're able to come up with solution then I guess you've learned something.
While loops are fine as an alternative.
If the question is simply to reach the final result ... yes, I learned a lot from this challenge.
But if the question is didactic, the ends do not justify the means.

I wasn't understanding the For Loops, and I think the exercise above all else is to better explore lesson theory.

I managed to come up with a result. I do not know if that is what was really requested.

It's time to see the Spoiler and find out the easiest way to do it!
Spoiler:

Code: Select all

words=input("Enter a text")

listt=[]
for word in words:
        listt.append(word)
print(listt)
print("*"*50)
listt.reverse()
print(listt)
Thx a lot Tim!
"I feel that in both art and music, it's not the success that matters but the pleasure it gives you. Focus on the pleasure and the learning will come naturally." - Brian Weston

tim
GL Blogger

Post#5 by tim » 21 Nov 2018, 02:01


I see you've found .reverse() on your own :D
aka Tin

         

Krikor
Forum Member

Post#6 by Krikor » 21 Nov 2018, 22:35


tim wrote:
21 Nov 2018, 02:01
I see you've found .reverse() on your own :D
I wasn't able to reverse the initial string order, using For Loop. :aw_shucks_purple:
As can be seen in the code below, the final inversion showed flaws.
Spoiler:

Code: Select all

words = ['0','1','2','3','4','5']
x=len(words)-1
listt=[]

for word in words:
    
    print(word, " tracking.word")
    
    listt.insert(x,word)
    
    print(x,word, " tracking.X.Word")
    print(listt, " tracking.list")
    x-=1

print(" ")    
print("*"*50)    

print(words, " Initial entry, no change")
print("*"*50)
    
print(listt, " Result, the order should have been reversed. But not.")   
I was using a counter to try to determine the input of the variable at a given position.

Then by chance, I discovered the .reverse (), and threw in the towel. :off_to_bed_purple:

Then, when I checked the answer, I understood that the Index should be fixed at position 0 (zero), and that with the new entries, it would be pushed to the right.

Even though I failed the challenge using For Loops, I learned a lot on the way.
"I feel that in both art and music, it's not the success that matters but the pleasure it gives you. Focus on the pleasure and the learning will come naturally." - Brian Weston

tim
GL Blogger

Post#7 by tim » 22 Nov 2018, 02:13


as long as you learn and play around it's all good :D
aka Tin

         


GIMP/GL:  Tutorials | Tips | Questions | Interests | Script Requests
Recommendations:  Index | Top 10
Resources:  Python-Fu Plug-ins | Scheme-Fu Scripts | GIMP
Python:  Lessons | Questions | 🡆 Exercises
The Arts:  GIMPed | GIMPed Photos | Other | Contests | Public Domain Images | Isolated Images | Blender
Inkscape
Social:  Say Hello | Anything Goes | Shares | FCSP

Post Reply
  • Similar Topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 1 guest