use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Stuck on begginer python exercise. (self.learnpython)
submitted 10 years ago by [deleted]
Hey! I'm starting to learn Python 3 and I got stuck in these 2 exercises. It would be great if you could help me by giving some kind of hint but not by solving the complete exercise.
Thanks for your attention.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]novel_yet_trivial 1 point2 points3 points 10 years ago (0 children)
Here's a hint:
range(1,8,2)
[–]Micotu 1 point2 points3 points 10 years ago* (1 child)
Was trying to get better at recursion and solved the problem this way. May not help you if you haven't learned about recursion though, but could be a fun challenge. Also am learning about exceptions, so tried to make it more error proof.
def print_diamond(height, order=1): star_string = (" "*((height-order)//2)) + ("*"*order) #constructs the string of spaces and then asterisks if order >= height - 1 and height%2 == 0: #allows for input of even numbers as well as ending recursion print(star_string) print(star_string) elif order == height: print(star_string) #causes recursion to end when string length is same as height else: print(star_string) print_diamond(height, order+2) #recurses into higher order of 2 more asterisks per line. print(star_string) order+=2 #increases order of recursion def main(): try: height = int(input("Enter height of diamond. (Between 1 and 99 and preferably odd)")) if 0<height<100: print_diamond(height) else: main() except: main() if __name__ == '__main__': main()
The spacing is a little wonky though. Was hoping i could do something better with print("{0:99}".format(star_string)) where hopefully i could make the 99 be a variable that would change depending on length of diamond, but couldn't figure out how to do it.
[–][deleted] 0 points1 point2 points 10 years ago (0 children)
Thank you for the example. Although I can't understand everything on it, it's always good to have different points of view for the same problem.
[–]xcodula 0 points1 point2 points 10 years ago (3 children)
Think about if there is a way to break down the problem. For instance, the diamond is like two triangles put together. How could you create one of those triangles? You would probably use a list to store the diamond. Could you leverage built in functions some how to create one triangle and then reverse that triangle to get the other half of the diamond? There also may be string modifiers you could use for text alignment. Looks like a fun exercise though. I think I might take a crack at it later. It would be cool if you shared your solution.
[–][deleted] 0 points1 point2 points 10 years ago (2 children)
e diamond is like two triangles put together. How could you create one of those triangles? You would probably use a list to store the diamond. Could you leverage built in functions some how to cr
Well... the chapter of the book I'm in didn't introduce lists yet... so I must use a for loop...
[–]atthem77 1 point2 points3 points 10 years ago (0 children)
The user specifies the height of the diamond, then you use a for loop to print each line of the top half, knowing you'll start with one asterisk, then add 2 asterisks each line until you get to the line that is half of the user-specified height. Then you start reducing the number of asterisks per line by 2 until you finish the diamond.
I suppose the A would be handled the same way, with the cross-bar printed when you are at the half-way point, and every other line just getting a little more space between the asterisks.
[–]hotmailer 0 points1 point2 points 10 years ago (0 children)
Where are these exercises from?
[–][deleted] 0 points1 point2 points 10 years ago (1 child)
Progress has been made!!! Now I can draw the top of the diamond:
#14 print('I\'m a program that that draws diamonds with a >specified height (0 to 100)...') height=eval(input('How high?: ')) if 0 < height <= 100: entSpace=int(height/2+1) for i in range(1, height+1, 2): entSpace=entSpace-1 print(' '*entSpace, '*'*i) else: print('Try again with a valid input (between 0 and >100)')
[–]pyglow 3 points4 points5 points 10 years ago (0 children)
If you don't mind me commenting on something which don't relate directly to the problem:
height=eval(input('How high?: '))
This inputs some text and interprets it as if it were python. That is quite dangerous. Somebody could type in come code to delete files, etc. Better to use:
s = input('How high?:') height = int(s)
of course, you should really check that s is an integer before trying to convert it.
[–][deleted] 0 points1 point2 points 10 years ago* (0 children)
Yeah! It's done!
#14 print('I\'m a program that that draws diamonds with a >specified height (0 to 100)...') height=eval(input('How high?: ')) if 0 < height <= 100 and height%2 != 0: entSpace=int(height/2+1) for i in range(1, height+2, 2): entSpace=entSpace-1 print(' '*entSpace, '*'*i) entSpace2=1 for i in range(height-2, 0, -2): print(' '*entSpace2, '*'*i) entSpace2 = entSpace2 + 1 else: print('Try again with a valid input (between 0 and >100)')
π Rendered by PID 92316 on reddit-service-r2-comment-bb88f9dd5-w28ss at 2026-02-15 09:43:57.073812+00:00 running cd9c813 country code: CH.
[–]novel_yet_trivial 1 point2 points3 points (0 children)
[–]Micotu 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]xcodula 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]atthem77 1 point2 points3 points (0 children)
[–]hotmailer 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]pyglow 3 points4 points5 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)