Loops and ASCII

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

Loops and ASCII

Postby el_manu » Wed Aug 28, 2013 6:24 am

Now this is ACTUALLY A TASK FOR SCHOOL, so for now i want guidance. I will post my codes, and i will need assistance on how to do thing but not the answer to the code. is that clear?

ok

I have a strong machine gun. I like to line up my lab assistants against a wall and shoot at them to see if they will die.
The power of the gun drops off by a factor of 10% every metre the bullets travel – so, in the first metre it's 90% of the original power.

I need to write a program that will do this using ASCII art. My program should ask the user for the total power of the gun, and the distance the person is standing from the end of the gun in metres. It should then draw the 'experiment' using the following characters:

If the power of the gun is greater than 50 units the rays should be an X
If the power of the gun is greater than 25 units the rays should be an x
If the power of the gun is greater than 10 units the rays should be a .
If the power of the gun falls to 10 units or below, the rays should be invisible (space)

Each character represents one metre and my gun looks like this: p==.

The bulets are only dangerous if they are above 10 units, at which strength they will demolish anything in their path.

So the program would look like this:
Code: Select all
Initial strength: 100
Distance to person: 20
                     \|/
p==XXXXXXxxxxxxx.....-*-
                     /|\
You have 1 less lab assistant!

Or, if the ray gun was less powerful and the person further away, it might look like this:
Code: Select all
Initial strength: 50
Distance to person: 20
                       |
p==xxxxxx.........    P|
                       |
The lab assistant survives!


Hint #1

Check the number of characters from the end of the gun to the lab assistant carefully! The lab assistant is either:

the * (asterisk) in the middle of the explosion if they are demolished;
the P if they survive and are still standing against the wall (the | character).

The length can be checked using copy and paste into a Python string, e.g.:

print(len('XXXXXXxxxxxxx.....-*'))

20


for the first example we have above.
Hint #2

Printing a backslash can be a bit tricky! The backslash is used to indicate a special interpretation of the following character, like newlines ('\n') and tabs ('\t'). This is called escaping the character.

Another example is if you want to put a single quote inside a single quoted string, you put a backslash before the quote, so Python knows you're not finishing the string yet, like this 'o\'clock'.

So if a backslash escapes the next character, what do you do if you actually want a backslash itself? Answer: use two backslashes:

print('\\')

\
Last edited by Mekire on Wed Aug 28, 2013 6:44 am, edited 1 time in total.
Reason: Edited in correct output.
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Loops and ASCII

Postby Mekire » Wed Aug 28, 2013 6:39 am

If you post the problem I can put it in the top post.

You post an image either by using the "upload" attachment tab found below the text box, or by hosting it at a third party site and using the image tags.


And as for your problem:

Well you have a range to the target; and a variable that changes everytime you move 1 unit.
Iterate over the range to the target; adjust the power variable and print the correct symbol.
Basic (for) loop asignment.

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

Re: Loops and ASCII

Postby el_manu » Wed Aug 28, 2013 6:48 am

i will work on it, and post my code on here when I am done or nearly done.

thx
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Loops and ASCII

Postby el_manu » Wed Aug 28, 2013 7:21 am

Code: Select all
a = int(input("Initial Strength: "))
b = int(input("Distance to person: "))
for d in range(b):
   a = 0.9 * a
   if a < 11:
      print(' ')
   elif a < 26:
      print("x")
   elif a > 50:
         print("X")
    elif a =


i will fix the rest up...any problems so far..and how do i print the answer on 1 line, and also how do i make the last elif to print the dots
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Loops and ASCII

Postby Mekire » Wed Aug 28, 2013 8:43 am

Actually you are going to have to construct the string, and then print it once you are done checking to see if the assistant dies. Use greater thans in your if statements and then have everything less than 10 fall under else.

Oh, and it is time to start using meaningful variable names. A variable representing distance shouldn't be called b; it should be called distance. A variable representing power shouldn't be called a; it should be called power; etc.

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

Re: Loops and ASCII

Postby el_manu » Thu Aug 29, 2013 10:56 am

Code: Select all
strength = int(input("Initial Strength: "))
distance = int(input("Distance to person: "))
x = "x"
X = "X"
blank = " "
dot = "."
count = 0
p = "p=="
while strength > 50:
  if distance == 0:
    print((count + 3) * blank + """\|/
p==""" + count * X + """-*-
 """ + (count + 2) * blank + "/|\\" + """
You have 1 less lab assistant!""")
  strength = strength * 0.9
  distance = distance - 1
  count = count + 1


this is my code above, it only works so far for over 50..will finish the rest, this is all good??
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Loops and ASCII

Postby el_manu » Thu Aug 29, 2013 11:01 am

i had this due for homework yesterday but I hadn't got it finished so yeah now no-one cares about this code, but it has just become something I wanna see done.
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Loops and ASCII

Postby el_manu » Thu Aug 29, 2013 11:43 am

i realised that i can't do the code like that and now i will never have this program. no!!!
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Loops and ASCII

Postby Mekire » Thu Aug 29, 2013 12:33 pm

Your first attempt with the for loop was closer.

Try to expand on the following:
Code: Select all
shoot_string = []
for distance in range(total_distance):
    power *= 0.9
    if power > 50:
        shoot_string.append("X")

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

Re: Loops and ASCII

Postby el_manu » Thu Aug 29, 2013 12:49 pm

Code: Select all
strength = int(input("Initial Strength: "))
distance = int(input("Distance to person: "))
x = "x"
X = "X"
blank = " "
dot = "."
count = 0
count1 = 0
count2 = 0
count3 = 0
count4 = 0
count5 = 0
count6 = 0
count7 = 0
count8 = 0
p = "p=="
while strength > 50:
   if distance == 0:
      print((count + 3) * blank + """\|/
p==""" + count * X + """-*-
 """ + (count + 2) * blank + "/|\\" + """
You have 1 less lab assistant!""")
   strength = strength * 0.9
   distance = distance - 1
   count = count + 1
   if strength > 25 and strength < 51:
      while strength > 25 and strength < 51:
         if distance == 0:
            print((count + 2) * blank + (count1 * blank) + """\|/
p==""" + ((count - 1) * X) + (count1 * x) + """-*-
""" + ((count - 1) + 3) * blank + (count1 * blank) + "/|\\" + """
You have 1 less lab assistant!""")
         strength = strength * 0.9
         distance = distance - 1
         count1 = count1 + 1
   if strength > 10 and strength < 26:
      while strength > 10 and strength < 26:
         if distance == 0:
            print((count + 2) * blank + (count1 * blank) + ((count2 - 1) * blank) + """\|/
p==""" + ((count - 1) * X) + (count1 * x) + ((count2 - 1) * dot) + """-*-
""" + (count + 2) * blank + (count1 * blank) + ((count2 - 1) * blank) + "/|\\" + """
You have 1 less lab assistant!""")
         strength = strength * 0.9
         distance = distance - 1
         count2 = count2 + 1


my code so far....i will finish it...
don't hate me but i am not doing it your way.
I still need to add if it is less than 11 and then I would have completed 1/4 of my code.

then i need to do if the number inputed started bewtween 25 and 51, and do all my nested while loops
then i need to do if number inputed started between 10 and 26
and then i need to do if number inputted started less than 11
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Loops and ASCII

Postby Mekire » Thu Aug 29, 2013 1:07 pm

You're going the wrong way. If you really want to write 500 lines for a program that should be less than 20, go right ahead. I'm sure not gonna check over it though. If you want advice then scrap this attempt and do it correctly. You need to stop avoiding things you don't understand and research them until you do.

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

Re: Loops and ASCII

Postby el_manu » Fri Aug 30, 2013 1:31 pm

I edited this message because i didn't give up as I wrote in this message before
Last edited by el_manu on Sat Aug 31, 2013 4:37 am, edited 2 times in total.
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Loops and ASCII

Postby el_manu » Sat Aug 31, 2013 4:36 am

I MADE THE CODE IN A BILLION LINES it works..now that i have completed it, can someone give me the easy way to do it..

if you wanna test it to make sure i'm not lying you can run it. The code is below

Code: Select all
strength = int(input("Initial Strength: "))
distance = int(input("Distance to person: "))
newdistance = distance
newstrength = strength
newastrength = strength
newaastrength = strength
newaastrength = strength
newaaastrength = strength
x = "x"
X = "X"
blank = " "
dot = "."
count = 0
count1 = 0
count2 = 0
count3 = 0
count4 = 0
count5 = 0
count6 = 0
count7 = 0
count8 = 0
count9 = 0
count10 = 0
count11 = 0
count12 = 0
count13 = 0
count14 = 0
count15 = 0
count16 = 0
count17 = 0
count18 = 0
p = "p=="
if strength > 50:
   while strength > 50:
      if distance == 1:
         print((count + 3) * blank + """\|/
p==""" + count * X + """-*-
 """ + (count + 2) * blank + "/|\\" + """
You have 1 less lab assistant!""")
      strength = strength * 0.9
      distance = distance - 1
      count = count + 1
      if strength > 25 and strength < 51:
         count = count - 1
         while strength > 25 and strength < 51:
            if distance == 1:
               print((count + 3) * blank + (count1 * blank) + """\|/
p==""" + ((count) * X) + (count1 * x) + """-*-
""" + ((count) + 3) * blank + (count1 * blank) + "/|\\" + """
You have 1 less lab assistant!""")
            strength = strength * 0.9
            distance = distance - 1
            count1 = count1 + 1
      if strength > 10 and strength < 26:
         while strength > 10 and strength < 26:
            if distance == 1:
               print((count + 3) * blank + (count1 * blank) + ((count2) * blank) + """\|/
p==""" + ((count) * X) + (count1 * x) + ((count2) * dot) + """-*-
""" + (count + 3) * blank + (count1 * blank) + ((count2) * blank) + "/|\\" + """
You have 1 less lab assistant!""")
            strength = strength * 0.9
            distance = distance - 1
            count2 = count2 + 1
      if strength < 11:     
         while strength < 11:
            if distance == 1:
               print((count + 3) * blank + (count1 * blank) + (count2 * blank) + (count3 * blank) + blank + """|
p==""" + (count * X) + (count1 * x) + (count2 * dot) + (count3 * blank) + """P|
""" + (count + 3) * blank + (count1 * blank) + (count2 * blank) + (count3 * blank) + blank + "|" + """
The lab assistant survives!""")
            strength = strength * 0.9
            distance = distance - 1
            count3 = count3 + 1
elif newstrength > 25 and newstrength < 51:
   while newstrength > 25 and newstrength < 51:
      if distance == 1:
         print((count4 + 3) * blank + (count5 * blank) + """\|/
p==""" + ((count4) * X) + (count5 * x) + """-*-
""" + ((count4) + 3) * blank + (count5 * blank) + "/|\\" + """
You have 1 less lab assistant!""")
      newstrength = newstrength * 0.9
      distance = distance - 1
      count5 = count5 + 1
      if newstrength > 10 and newstrength < 26:
         count5 = count5 - 1
         while newstrength > 10 and newstrength < 26:
            if distance == 1:
               print((count4 + 3) * blank + (count5 * blank) + ((count6) * blank) + """\|/
p==""" + ((count4) * X) + (count5 * x) + ((count6) * dot) + """-*-
""" + (count4 + 3) * blank + (count5 * blank) + ((count6) * blank) + "/|\\" + """
You have 1 less lab assistant!""")
            newstrength = newstrength * 0.9
            distance = distance - 1
            count6 = count6 + 1
      if newstrength < 11:     
         while newstrength < 11:
            if distance == 0:
               print((count4 + 3) * blank + (count5 * blank) + (count6 * blank) + (count7 * blank) + blank + """|
p==""" + (count4 * X) + (count5 * x) + (count6 * dot) + (count7 * blank) + """P|
""" + (count4 + 3) * blank + (count5 * blank) + (count6 * blank) + (count7 * blank) + blank + "|" + """
The lab assistant survives!""")
            newstrength = newstrength * 0.9
            distance = distance - 1
            count7 = count7 + 1
elif newastrength > 10 and newastrength < 26:
   while newastrength > 10 and newastrength < 26:
      if distance == 1:
         print((count8 + 3) * blank + (count9 * blank) + ((count10) * blank) + """\|/
p==""" + ((count8) * X) + (count9 * x) + ((count10) * dot) + """-*-
""" + (count8 + 3) * blank + (count9 * blank) + ((count10) * blank) + "/|\\" + """
You have 1 less lab assistant!""")
      newastrength = newastrength * 0.9
      distance = distance - 1
      count10 = count10 + 1
      if newstrength < 11:     
         count10 = count10 - 1
         while newastrength < 11:
            if distance == 0:
               print((count8 + 3) * blank + (count9 * blank) + (count10 * blank) + (count11 * blank) + blank + """|
p==""" + (count8 * X) + (count9 * x) + (count10 * dot) + (count11 * blank) + """P|
""" + (count8 + 3) * blank + (count9 * blank) + (count10 * blank) + (count11 * blank) + blank + "|" + """
The lab assistant survives!""")
            newastrength = newastrength * 0.9
            distance = distance - 1
            count11 = count11 + 1
elif newaastrength < 11:     
         while newaastrength < 11:
            if distance == 0:
               print((count12 + 3) * blank + (count13 * blank) + (count14 * blank) + (count15 * blank) + blank + """|
p==""" + (count12 * X) + (count13 * x) + (count14 * dot) + (count15 * blank) + """P|
""" + (count12 + 3) * blank + (count13 * blank) + (count14 * blank) + (count15 * blank) + blank + "|" + """
The lab assistant survives!""")
            newaastrength = newaastrength * 0.9
            distance = distance - 1
            count15 = count15 + 1
elif newdistance == 0:
   if newaaastrength > 11:
        print((count16 + 3) * blank + (count17 * blank) + ((count18) * blank) + """\|/
p==""" + ((count16) * X) + (count17 * x) + ((count18) * dot) + """-*-
""" + (count16 + 3) * blank + (count17 * blank) + ((count18) * blank) + "/|\\" + """
You have 1 less lab assistant!""")
   if newaaastrength < 11:
      print((count16 + 3) * blank + (count17 * blank) + (count18 * blank) + (count19 * blank) + blank + """|
p==""" + (count16 * X) + (count17 * x) + (count18 * dot) + (count19 * blank) + """P|
""" + (count16 + 3) * blank + (count17 * blank) + (count18 * blank) + (count19 * blank) + blank + "|" + """
The lab assistant survives!""")
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Loops and ASCII

Postby micseydel » Sat Aug 31, 2013 4:49 am

You should be trying to do it yourself. First, use lists instead of enumerated variable names. You can come back and ask again after doing that for yourself.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1497
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Loops and ASCII

Postby el_manu » Sat Aug 31, 2013 4:58 am

I thought since I have already completed the question using the code of my own, and since the code is just something for me now that someone would tell me the answer a different way...i guess i thought wrong.....I have already completed the question my way so i'm not wasting any of my time doing it another way, i just wanted to know the answer a different way
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Loops and ASCII

Postby el_manu » Sat Aug 31, 2013 5:10 am

HOW do you say

while a > 25 and a < 51

in a for loop???
is it:

for a in range(25,51)
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Loops and ASCII

Postby Mekire » Sat Aug 31, 2013 5:12 am

If you had listened to advice and tried to do it correctly from the start instead of doing it your own (wrong) way, then you would have figured this out by now.

Go ask your classmate:
http://www.reddit.com/r/learnpython/comments/1lbjzz/help_program_is_printing_out_extra/

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

Re: Loops and ASCII

Postby el_manu » Sat Aug 31, 2013 5:16 am

what!!!!!!! he's not my classmate. My teacher must have taken the question from somewhere, but i already failed that homework task anyway by not doing it so it isn't for anything...and he definitely isn't my classmate hahah. i am now determined to find where my teacher gets his questions from
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Loops and ASCII

Postby Mekire » Sat Aug 31, 2013 5:19 am

Regardless, he is doing it a much better way, so look at his approach.

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

Re: Loops and ASCII

Postby el_manu » Sat Aug 31, 2013 5:25 am

how come now you haven't told me the asnwer in a simpler way, since i gave u my code that works, but before when i posted a retarted code that worked you gave a different answer to them, and that asnwer was better??? u make no sense sometimes
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Next

Return to General Coding Help

Who is online

Users browsing this forum: Mekire, snippsat and 4 guests