Help again

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

Help again

Postby el_manu » Tue Aug 20, 2013 9:05 am

Suppose Sam is trying to walk from his house to his friend's house, which is 8 metres away. First he must get half way there (4 metres), then a quarter (2 metres) (as this is half of what remains to travel), then an eighth (1 metre), then a sixteenth (0.5 metres) etc etc. How can he ever reach his destination?

Implement a program that asks the user for a distance and a number of steps, and prints out the remaining distance assuming the person travels in this manner.

Your program should work like this:
Enter a distance: 8
Number of steps: 5
Distance remaining = 0.25 m



And this:
Enter a distance: 2.5
Number of steps: 4
Distance remaining = 0.15625 m


my attempt is

Code: Select all
a = float(input('Enter a distance: '))
b = int(input("Number of steps: "))
print("Distance remaining =", (a / ( 2 ^ b )), "m")


but i don't know what i was meant to do in the 3rd line.

Also a step is a single movement (which might be many meters). Try to think of it as logical steps in a process, rather than a physical walking step.

So step one is 4 meters, step 2 is 2 meters, etc, etc.
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Help again

Postby el_manu » Tue Aug 20, 2013 9:23 am

Code: Select all
m = "m"
a = float(input('Enter a distance: '))
b = int(input("Number of steps: "))
print("Distance remaining =", (a / 2 ** b), m)


figured it out don't worry
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Help again

Postby Mekire » Tue Aug 20, 2013 9:26 am

For this problem you need to write a for loop that loops over the number of steps you take. Each iteration divide the remaining distance by 2. I suggest you use the augmented assignment operator "/=".

Take another shot with a loop and we'll work from there.

-Mek

Edit:
Your answer is indeed correct; however your instructor was probably aiming at you using a for loop. Anyway, nice work. :)
Code: Select all
for step in range(steps):
    distance /= 2.0
User avatar
Mekire
 
Posts: 828
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Help again

Postby el_manu » Tue Aug 20, 2013 9:45 am

i think he probably was looking for a loop. Thanks for telling me that.
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am


Return to General Coding Help

Who is online

Users browsing this forum: Majestic-12 [Bot] and 2 guests