How to u use the .append

This is the place for queries that don't fit in any of the other categories.

How to u use the .append

Postby el_manu » Sat Aug 31, 2013 12:25 pm

i have decided to take mekire's advice in figuring out loops and ASCII but, i don't know how to use .append
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: How to u use the .append

Postby el_manu » Sat Aug 31, 2013 12:32 pm

and can someone help me understand what this guy has done:

http://pastebin.com/4bsczAdN
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: How to u use the .append

Postby Mekire » Sat Aug 31, 2013 12:51 pm

Append is the basic method for adding items to the datatype list.

You create an empty list like this:
Code: Select all
a = []
You add items to it like this:
Code: Select all
a.append("something")
a.append("something else")
a.append("one more thing")

Let's check our list:
Code: Select all
>>> print(a)
['something', 'something else', 'one more thing']

The guy is adjusting his power every time through the loop, and then appending the correct symbol to the list based on the current power.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: How to u use the .append

Postby el_manu » Sat Aug 31, 2013 1:26 pm

i think this works..


Code: Select all
strength = int(input("Initial strength: "))
distance = int(input("Distance to person: "))
strength1 = strength
Hallelouya_length = []
Hallelouya_length1 = []
blank = " "
for i in range(0,distance-1):
     strength *= 0.9
     if strength > 50:
         Hallelouya_length.append('X')
     elif strength > 25:
         Hallelouya_length.append('x')
     elif strength > 10:
         Hallelouya_length.append('.')
     else:
         Hallelouya_length.append(' ')
for d in range(0,distance-2):
     strength1 *= 0.9
     if strength1 > 50:
         Hallelouya_length1.append('X')
     elif strength1 > 25:
         Hallelouya_length1.append('x')
     elif strength1 > 10:
         Hallelouya_length1.append('.')
     else:
         Hallelouya_length1.append(' ')
if strength > 10:
    print(' '*(3+len(Hallelouya_length))+' |')
    print('p=='+''.join(Hallelouya_length)+'P|')
    print(' '*(3+len(Hallelouya_length))+' |')
    print("The lab assistant survives!")
else:
    print(' '*(3+len(Hallelouya_length1))+'\|/')
    print('p=='+''.join(Hallelouya_length1)+'-*-')
    print(' '*(3+len(Hallelouya_length1))+'/|\\')
    print("You have 1 less lab assistant!")
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: How to u use the .append

Postby Mekire » Sat Aug 31, 2013 1:55 pm

Gives the wrong result. You don't need to create the list twice; only once. This will create the true ray list; then check if the last element of the ray is a empty space or not. If it is a space the assistant survives. After you determine whether he survives or not you need to add the assistants symbols to the list appropriately. This is the issue that the writer of that reddit thread was having. See my response in his thread for the solution.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: How to u use the .append

Postby el_manu » Sat Aug 31, 2013 2:13 pm

Code: Select all
strength = int(input("Initial strength: "))
distance = int(input("Distance to person: "))
strength1 = strength
Hallelouya_length = []
Hallelouya_length1 = []
blank = " "
for i in range(0,distance):
     strength *= 0.9
     if strength > 50:
         Hallelouya_length.append('X')
     elif strength > 25:
         Hallelouya_length.append('x')
     elif strength > 10:
         Hallelouya_length.append('.')
     else:
         Hallelouya_length.append(' ')
for d in range(0,distance):
     strength1 *= 0.9
     if strength1 > 50:
         Hallelouya_length1.append('X')
     elif strength1 > 25:
         Hallelouya_length1.append('x')
     elif strength1 > 10:
         Hallelouya_length1.append('.')
     else:
         Hallelouya_length1.append(' ')
if strength <= 10:
    Hallelouya_length = Hallelouya_length[0:-1]
    print(' '*(3+len(Hallelouya_length))+' |')
    print('p=='+''.join(Hallelouya_length)+'P|')
    print(' '*(3+len(Hallelouya_length))+' |')
    print("The lab assistant survives!")
elif strength > 10:
    Hallelouya_length1 = Hallelouya_length1[0:-2]
    print(' '*(3+len(Hallelouya_length1))+'\|/')
    print('p=='+''.join(Hallelouya_length1)+'-*-')
    print(' '*(3+len(Hallelouya_length1))+'/|\\')
    print("You have 1 less lab assistant!")


this is actually the answer, i fixed it.

it has been proven correct so don't tell me its wrong. i sent this to my teacher and even though he didn't want it because it was already due and stuff, he said it was correct
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: How to u use the .append

Postby Mekire » Sat Aug 31, 2013 2:35 pm

Yes, it is giving the correct answer now. You still don't need to generate the list twice. You just need to generate it once and slice properly at the end.

Code: Select all
power = int(input("Initial Strength: "))
total_distance = int(input("Distance to person: "))

shoot_string = []
for distance in range(total_distance):
    power *= 0.9
    if power > 50:
        shoot_string.append('X')
    elif power > 25:
        shoot_string.append('x')
    elif power > 10:
        shoot_string.append('.')
    else:
        shoot_string.append(' ')

if shoot_string[-1] == " ":
    shoot_string[-1:] = ["P","|"]
    print("{}{}".format(" "*(len(shoot_string)-1),"|"))
    print("".join(shoot_string))
    print("{}{}".format(" "*(len(shoot_string)-1),"|"))
    print("The lab assistant survives!")
else:
    shoot_string[-2:] = ["-","*","-"]
    print("{}{}".format(" "*(len(shoot_string)-3),"\\|/"))
    print("".join(shoot_string))
    print("{}{}".format(" "*(len(shoot_string)-3),"/|\\"))
    print("You have 1 less lab assistant!")

el_manu wrote:it has been proven correct so don't tell me its wrong.
You need to figure out what it is you want. If you don't care about improving then why are you even here?

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], Yoriz and 2 guests