QUOTE(dstl1128 @ Feb 23 2017, 09:56 AM)
oh.. sorry I was placing it on the incorrect version of compiler. Thank you master!
For Loops in Python
|
Feb 23 2017, 10:03 AM
|
Junior Member
79 posts Joined: Apr 2012 |
|
|
|
|
Feb 23 2017, 12:24 PM
|
Junior Member
635 posts Joined: Jul 2006 |
|
|
Feb 23 2017, 01:18 PM
|
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. |
|
Feb 23 2017, 07:35 PM
|
Junior Member
79 posts Joined: Apr 2012 |
QUOTE(dstl1128 @ Feb 23 2017, 01:18 PM) Probably a better approach would be Question, if that will be the shortest way, then how can we write it in detail?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. 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 |
|
Feb 23 2017, 09:51 PM
|
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. |
|
Feb 23 2017, 10:00 PM
|
Junior Member
79 posts Joined: Apr 2012 |
QUOTE(dstl1128 @ Feb 23 2017, 09:51 PM) What kind of detail you want? 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 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. 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 Attached thumbnail(s) |
|
Feb 24 2017, 12:58 PM
|
Junior Member
79 posts Joined: Apr 2012 |
QUOTE(dstl1128 @ Feb 23 2017, 09:56 AM) 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 |
|
Feb 24 2017, 02:11 PM
|
Senior Member
4,463 posts Joined: Jan 2003 |
QUOTE(effytheman @ Feb 24 2017, 12:58 PM) Sorted it out and I came to this: Good. However your last line won't be printing something likestrings = [] 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 CODE { 1: 'a', 2: 'B', ... , 10: 'Z' } |
|
|
|
Feb 24 2017, 04:26 PM
|
Junior Member
79 posts Joined: Apr 2012 |
|
|
Feb 24 2017, 08:54 PM
|
Senior Member
4,463 posts Joined: Jan 2003 |
|
|
Feb 26 2017, 10:15 PM
|
Junior Member
79 posts Joined: Apr 2012 |
guys..anybody awake or online right now? Can I ask for your help in my quiz?
|
|
Feb 26 2017, 11:25 PM
|
Junior Member
79 posts Joined: Apr 2012 |
|
|
Feb 26 2017, 11:30 PM
Show posts by this member only | IPv6 | Post
#33
|
Senior Member
1,196 posts Joined: Apr 2015 |
name ?
|
|
Feb 26 2017, 11:32 PM
|
Junior Member
79 posts Joined: Apr 2012 |
|
|
|
|
Feb 26 2017, 11:34 PM
|
Junior Member
79 posts Joined: Apr 2012 |
|
|
Feb 26 2017, 11:43 PM
Show posts by this member only | IPv6 | Post
#36
|
Senior Member
1,196 posts Joined: Apr 2015 |
|
|
Feb 26 2017, 11:46 PM
|
Junior Member
79 posts Joined: Apr 2012 |
|
|
Feb 26 2017, 11:50 PM
Show posts by this member only | IPv6 | Post
#38
|
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
|
|
Feb 27 2017, 12:00 AM
|
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.... |
|
Feb 27 2017, 12:02 AM
|
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: | 0.0167sec
0.16
6 queries
GZIP Disabled
Time is now: 29th March 2024 - 06:16 AM |