Jump to content

loop help

- - - - -

  • Please log in to reply
3 replies to this topic

#1
apejam

apejam

    Newbie

  • Members
  • Pip
  • 6 posts
Hello all I am new to the boards as well as to programming!

I am currently taking a college intro to programming course but the professor blatantly sucks at explaining concepts, so I was hoping I could get some help from the programmers of this board!

I have to write a program that gets the miles per gallon for 5 legs of a trip inputted by the user. I can easily get one leg.. but how do i get the other 4? I have to use a while loop but have no idea how to implement it into such a program. how do i connect these?

** I really want to understand the concepts behind this so if someone could point me in the right direction or explain thoroughly as opposed to just doing it for me I would be greatly appreciative.

this is what I have thus far.

leg = 0

legCounter = 5


while leg <= legCounter:

    leg = leg + 1


#the user input amount of miles driven by user	

milesDriven = int(input('how many miles have you driven?'))



#the amount of gallons the user has input

gallonsUsed = int(input('how many gallons have you used?'))



# the miles per gallon

milesPerGallon = int(milesDriven / gallonsUsed)


# display the  miles per gallon	

print ('your miles per gallon are')

print  (str(milesPerGallon))


input()


#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 890 posts
  • Location:::1
This might sound odd, but what is a leg of a trip? Do you mean you have to ask the user(s) 5 times and then compute total mileage, gallon usage, etc.. ?

Also, here you can read how to format strings with print() function.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
apejam

apejam

    Newbie

  • Members
  • Pip
  • 6 posts
Yeah it is very odd. My professor is British so the entire class of Americans had to have a "leg of a trip" explained to them. Basically it is just a part of a trip. You start on leg 1, but when you stop or hit a planned marking point you begin the second leg of the trip and so on until you reach your destination. apparently it's common term. "how long was your trip?" "It was just a few legs"
So on top of learning how to program the class also has to learn how to decipher British lingo.

Anyways I figured out how to write the program an hour or so after posting this, it's not the best looking code.. but dammit, it functions and runs :)
leg = 1

legCounter = 6


# while leg is less than the leg counter continue the loop

while leg < legCounter:

   

    

	

	

#the user input amount of miles driven by user	

	milesDriven = int(input('how many miles have you driven?'))



#the amount of gallons the user has input

	gallonsUsed = int(input('how many gallons have you used?'))



# the miles per gallon

	milesPerGallon = int(milesDriven / gallonsUsed)

   

# display which leg of the trip there is

	print ('for leg' + (' ') + str(leg) + (' ') + ('your miles per gallon are'))


	

	

	# count up the legs until it is equal to the fifth and final leg to break the loop

	leg = leg + 1

	

	#display the miles per gallon of each leg.

	print  (str(milesPerGallon))

	print ()


	

input('Done... press any key to exit program')



#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 890 posts
  • Location:::1
I though of that, leg part, too but wasn't really sure. Anyways, good job on making it work. :)
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users