Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with assignment
#1
Hi, I have this assignment to do and no idea where to start...
You ask the user to enter a random number, which expresses the size of the 'Christmas tree'. The trunk is not included in the size of the tree.
Example:
size = 5

(5 is the 'input number', that the user has enetered.)
Then in the terminal you get this:
X
XXX
XXXXX
XXXXXXX
XXXXXXXXX
X

Thanks in advance for your help!
Reply
#2
start by writing one line of code.
We will be glad to help, but cannot write it for you.
First think of the variables that have to be addressed:
  • Starts and ends with just one x
  • Each line has one less space per line and two more x's
  • bottom line same as first line (different way of saying step 1)
Reply
#3
Here's what I've done so far...

def print_segment(n, total_width):
    for size in range(1, n+1, 2):
        print((size * "X").center(total_width))

def print_spruce(size):
    for i in range(3, size+1, 2):
       print_segment(i, size)

print("Write the size of the spruce:")
n = int(input())
print_spruce(n)
Reply
#4
You don't really need print_segment. You don't want to loop there, you just want the print statement. So get rid of print_segment and just replace it with the print statement. Then your loop in print_spruce should start with 1. Then you are just missing one bit you should be able to figure out yourself.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(Nov-17-2018, 11:19 PM)ichabod801 Wrote: You don't really need print_segment. You don't want to loop there, you just want the print statement. So get rid of print_segment and just replace it with the print statement. Then your loop in print_spruce should start with 1. Then you are just missing one bit you should be able to figure out yourself.

I did this...

print("Write the size of the spruce:")
n = int(input())
print_spruce(n)
 
def print_spruce(size):
    for i in range(1, size+1, 2):
       print(i, size)
And then got this...

Write the size of the spruce:
4
Traceback (most recent call last):
  File "assign.py", line 3, in <module>
    print_spruce(n)
NameError: name 'print_spruce' is not defined
What am I doing wrong?

rint("Write the size of the spruce:")
n = int(input())
print_spruce(n)

def print_spruce(n):
    for size in range(1, n+1, 2):
        print((size * "X").center(n))

print_spruce(n)
terminal:
Write the size of the spruce:
9
Traceback (most recent call last):
  File "assign.py", line 3, in <module>
    print_spruce(n)
NameError: name 'print_spruce' is not defined
Reply
#6
remove line #3
i.e. you try to use print_spruce() before it is defined.
Even better
def print_spruce(n):
    for size in range(1, n+1, 2):
        print((size * "X").center(n))

n = int(input("Write the size of the spruce:"))
print_spruce(n)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Oh my god. Thanks a lot.

Number of Xs in the penultimate line ... p
entered numberof the user .... n
total number of the lines... t

So now I have two more 'issues':
- get one line more than the entered number from the user (example: entered number(n) of the user is 5, I got 6 lines.)

- modify the range (?) so that the number of the X in the last is 1. Should I use while loop here? 'When you came to the number of Xs > n+4, display number of Xs as in the first line.

def print_spruce(n):
    for size in range(1, n+5, 2):
        print((size * "X").center(n+5))
 
n = int(input("Write the size of the spruce:"))
print_spruce(n)
terminal:

Write the size of the spruce:5
    X
   XXX
  XXXXX
 XXXXXXX
XXXXXXXXX
The 'odd lines' must be blank.
And the last odd line must be same as the first line.

Odd lines must be blank = spacing between lines with Xs. So can I use '\n '?

def print_spruce(n):
    for size in range(1, n+5, 2):
        (size * "X").center(n+5)
       for x in range(1, n+5, 2):print "line \n"

n = int(input("Write the size of the spruce:"))
print_spruce(n)
terminal:

 File "assign.py", line 4
    for x in range(1, n+5, 2):print "line \n"
                                            ^
IndentationError: unindent does not match any outer indentation level
Reply
#8
Little update:

def print_spruce(n):
       for i in range(1, n+1): 
              print (" " *(n-i) + "X"*(2*i-1))
              print (" " * (n-1) + "X")
n = int(input("Write the size of the spruce:"))
print_spruce(n)
terminal:
Write the size of the spruce:5
    X
    X
   XXX
    X
  XXXXX
    X
 XXXXXXX
    X
XXXXXXXXX
    X
So, what should I change?

I think this is the cloest to my goal:

def print_spruce(n):
       for i in range(1, n+1, 2): 
              print (" " *(n-i) + "X"*(2*i-1))
              print (" " * (n-1) + "X")
n = int(input("Write the size of the spruce:"))
print_spruce(n+1)
terminal:
Write the size of the spruce:5
     X
     X
   XXXXX
     X
 XXXXXXXXX
     X
So, where are the mistakes?
Reply
#9
The first one looks good, but you should unindent the second print statement so that it is not part of the loop. The second print statement prints the trunk. You only want to do that once, after the rest of the tree is printed. Indented as it is now, it's in the loop, so it prints every row, instead of just once at the end.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
just for fun, another way to do this (requires python 3.6 or newer):
def tree():
    while True:
        spaces = ' '
        xes = 'x'
        height = (input('Enter height of tree: '))
        if height.isdigit():
            height = int(height)
            spaces = spaces * (height + 5)
            final = f'{spaces}x'
            break
        else:
            print("That's not a digit")
    while height:
        print(f'{spaces}{xes}')
        xes = f'{xes}xx'
        spaces = spaces[:-1]
        height -= 1
    print(final)    

if __name__ == '__main__':
    tree()
test:
Output:
Enter height of tree: 8 x xxx xxxxx xxxxxxx xxxxxxxxx xxxxxxxxxxx xxxxxxxxxxxxx xxxxxxxxxxxxxxx x
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020