Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 For Loops in Python

views
     
TSeffytheman
post Feb 23 2017, 10:03 AM

Getting Started
**
Junior Member
79 posts

Joined: Apr 2012
QUOTE(dstl1128 @ Feb 23 2017, 09:56 AM)
Working very fine.

[attachmentid=8516591]
*
oh.. sorry I was placing it on the incorrect version of compiler. rclxub.gif


Thank you master! rclxms.gif
angch
post Feb 23 2017, 12:24 PM

On my way
****
Junior Member
635 posts

Joined: Jul 2006
QUOTE(dstl1128 @ Feb 23 2017, 12:10 AM)
CODE
strings = { (i+1): raw_input('%d.Enter the word or string: ' % (i+1)).__getattribute__(('lower','upper')[i%2])() for i in range(10) }
print '\n'.join(strings.values())
print strings

*
Good use of __getattribute__. Learnt something new, thanks!
dstl1128
post Feb 23 2017, 01:18 PM

Look at all my stars!!
*******
Senior Member
4,463 posts

Joined: Jan 2003
Probably a better approach would be
CODE
strings = { (i+1): getattr(raw_input('%d.Enter the word or string: ' % (i+1)), ('lower','upper')[i%2])() for i in range(10) }


__getattribute__ looks ugly.

TSeffytheman
post Feb 23 2017, 07:35 PM

Getting Started
**
Junior Member
79 posts

Joined: Apr 2012
QUOTE(dstl1128 @ Feb 23 2017, 01:18 PM)
Probably a better approach would be
CODE
strings = { (i+1): getattr(raw_input('%d.Enter the word or string: ' % (i+1)), ('lower','upper')[i%2])() for i in range(10) }


__getattribute__ looks ugly.
*
Question, if that will be the shortest way, then how can we write it in detail?


Example:

numbers= [1,2,3,4,5,6,7,8,9,10]
strings={}
for item in numbers:
strings[item]= raw_input("%s.Enter the word or string:" % item)
for key in strings:
if key % 2 == 1:
print strings[key].lower()

else:
print strings[key].upper()


#this is just an external part in which to check the keys are partnered with the user-inputs
print strings



rclxub.gif
dstl1128
post Feb 23 2017, 09:51 PM

Look at all my stars!!
*******
Senior Member
4,463 posts

Joined: Jan 2003
What kind of detail you want?

You can get the input an then straight do the checking and upper/lower case them before storing.

OR you can get the input and store into a container first. Then loop the container to change the case.

Then when you print, you can just loop again on the container and print.
Or use some ready-made method or built-in function to combine the output and print once without loop.

And then when I say loop,
you can use an integer counter with 'while'
OR you can use a list/generator and iterate each of the elements.




TSeffytheman
post Feb 23 2017, 10:00 PM

Getting Started
**
Junior Member
79 posts

Joined: Apr 2012
QUOTE(dstl1128 @ Feb 23 2017, 09:51 PM)
What kind of detail you want?

You can get the input an then straight do the checking and upper/lower case them before storing.

OR you can get the input and store into a container first. Then loop the container to change the case.

Then when you print, you can just loop again on the container and print.
Or use some ready-made method or built-in function to combine the output and print once without loop.

And then when I say loop,
you can use an integer counter with 'while'
OR you can use a list/generator and iterate each of the elements.
*
Detail is something like the one below...in detail per line as I copy and study it in the compiler. https://repl.it/languages/python

Code works, great.. however, since I'm new with this, id be more than happy if you can show me the longer path so I can understand each smile.gif











Attached thumbnail(s)
Attached Image
TSeffytheman
post Feb 24 2017, 12:58 PM

Getting Started
**
Junior Member
79 posts

Joined: Apr 2012
QUOTE(dstl1128 @ Feb 23 2017, 09:56 AM)
Working very fine.

[attachmentid=8516591]
*
Sorted it out and I came to this:


strings = []
for i in range(1,11):
strings.append(raw_input("Enter the word or string "+ ": "))
for i in range(10):
if i % 2 == 1:
print strings[i].upper()
else:
print strings[i].lower()

print strings
dstl1128
post Feb 24 2017, 02:11 PM

Look at all my stars!!
*******
Senior Member
4,463 posts

Joined: Jan 2003
QUOTE(effytheman @ Feb 24 2017, 12:58 PM)
Sorted it out and I came to this:
strings = []
for i in range(1,11):
    strings.append(raw_input("Enter the word or string "+ ": "))
for i in range(10):
    if i % 2 == 1:
        print strings[i].upper()
    else:
print strings[i].lower()
   
print strings
*
Good. However your last line won't be printing something like
CODE
{ 1: 'a', 2: 'B', ... , 10: 'Z' }



TSeffytheman
post Feb 24 2017, 04:26 PM

Getting Started
**
Junior Member
79 posts

Joined: Apr 2012
QUOTE(dstl1128 @ Feb 24 2017, 02:11 PM)
Good. However your last line won't be printing something like
CODE
{ 1: 'a', 2: 'B', ... , 10: 'Z' }

*
Hmm.. I missed something, I cant recall where.. shocking.gif
dstl1128
post Feb 24 2017, 08:54 PM

Look at all my stars!!
*******
Senior Member
4,463 posts

Joined: Jan 2003
QUOTE(effytheman @ Feb 24 2017, 04:26 PM)
Hmm.. I missed something, I cant recall where..  shocking.gif
*
user posted image
TSeffytheman
post Feb 26 2017, 10:15 PM

Getting Started
**
Junior Member
79 posts

Joined: Apr 2012
guys..anybody awake or online right now? Can I ask for your help in my quiz?
TSeffytheman
post Feb 26 2017, 11:25 PM

Getting Started
**
Junior Member
79 posts

Joined: Apr 2012
Need Help.


Attached thumbnail(s)
Attached Image
GSCboy
post Feb 26 2017, 11:30 PM

Regular
******
Senior Member
1,196 posts

Joined: Apr 2015
name ?
TSeffytheman
post Feb 26 2017, 11:32 PM

Getting Started
**
Junior Member
79 posts

Joined: Apr 2012
QUOTE(GSCboy @ Feb 26 2017, 11:30 PM)
name ?
*
Is it the answer?


Also please please this one below...


Attached thumbnail(s)
Attached Image
TSeffytheman
post Feb 26 2017, 11:34 PM

Getting Started
**
Junior Member
79 posts

Joined: Apr 2012
QUOTE(GSCboy @ Feb 26 2017, 11:30 PM)
name ?
*
Another one sir..please? will be forever thanking you smile.gif


Attached thumbnail(s)
Attached Image
GSCboy
post Feb 26 2017, 11:43 PM

Regular
******
Senior Member
1,196 posts

Joined: Apr 2015
QUOTE(effytheman @ Feb 26 2017, 11:34 PM)
Another one sir..please? will be forever thanking you smile.gif
*
Just use a for in loop to loop through the dictionaries and convert the grade % into numeric values before u print it
TSeffytheman
post Feb 26 2017, 11:46 PM

Getting Started
**
Junior Member
79 posts

Joined: Apr 2012
QUOTE(GSCboy @ Feb 26 2017, 11:43 PM)
Just use a for in loop to loop through the dictionaries and convert the grade % into numeric values before u print it
*
How sir? Could you please provide me the answer? please...
GSCboy
post Feb 26 2017, 11:50 PM

Regular
******
Senior Member
1,196 posts

Joined: Apr 2015
Bro, you should learn to do it yourself, spoonfeeding is not the way to go in programming, u will hardly learn, at least try yourself and type the code out to see your output and where it goes wrong
TSeffytheman
post Feb 27 2017, 12:00 AM

Getting Started
**
Junior Member
79 posts

Joined: Apr 2012
QUOTE(GSCboy @ Feb 26 2017, 11:50 PM)
Bro, you should learn to do it yourself, spoonfeeding is not the way to go in programming, u will hardly learn, at least try yourself and type the code out to see your output and where it goes wrong
*
strings = []
for i in range(1,1000):
strings.append(raw_input("Enter your Name "+ ": "))
for i in range(1000):
strings.append(raw_input("Enter your Grade "+ ": "))

print strings


aint working bro....
TSeffytheman
post Feb 27 2017, 12:02 AM

Getting Started
**
Junior Member
79 posts

Joined: Apr 2012
QUOTE(GSCboy @ Feb 26 2017, 11:50 PM)
Bro, you should learn to do it yourself, spoonfeeding is not the way to go in programming, u will hardly learn, at least try yourself and type the code out to see your output and where it goes wrong
*
strings = []
for i in range(0,1000):
strings.append(raw_input("Enter your Name "+ ": "))
ss = raw_input("")
for i in range(1000):
strings.append(raw_input("Enter your Grade "+ ": "))
print strings[i].upper()

print('Converted strings:')
for i in range(0,10):
 print(strings[i])


same...

 

Change to:
| Lo-Fi Version
0.0167sec    0.16    6 queries    GZIP Disabled
Time is now: 29th March 2024 - 06:16 AM