Python Forum
While Loop; Can Else Point Back To Initial Input Command?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
While Loop; Can Else Point Back To Initial Input Command?
#1
Guys and gals, I have lost my ever-loving mind. I was given a homework assignment (Intro to Python) and I have mostly figured this out, but I cannot get one thing right: When cents < 0, point back to require the input again (for now I have it break, but even that is broken). This is driving me crazy, there is something simple I am missing, I know it and I need a little nudge to get me to see it.

The assignment states I have to calculate a user defined value from 0-99 and kick back how many quarters, dimes, nickels and pennies are required to make that value (using highest value coins first, so if a single coin can hold a value/remainder, it must before a lower coin can fill any remaining void).

My issue is that even though I did just that, the teacher has kicked it back and states I need to have the input put back in when the value of cents < 0. I took a pseudo-code course last semester, but then was off all Summer, so I have forgotten a lot of the logic.

Python is hard, but I a pushing forward and am not asking anyone to do the work for me. I cannot figure out my logic here, I am missing something. I have made this far and dagburnit after two nights on this I have to move on and accept I don't see it. Please, someone, give me a hint, where have I failed logically to loop this back so I can learn from this horrible, horrible second program (our first program was Hello World!, and now this; yes, this is Week 2 homework). I bet it is easy... I just don't see it. My code is below, and real talk, thank all of you for what you do. I doubt many people aren't grateful, but trust me when I say that I am. Reading these forums has taught me so much and I hate I have to bother you for this little problem, but I must move on and learn from this; I can't hold up any longer, it is due in four days and I have given it my all. R.I.P. My Logic.

Cheers,

~Rod

# program to get user-defined input (0-99)
# program will calculate number of coins required to make exact change using highest value coins first

# get user input
# use int; float will not round decimals accurately for req'd math
cents = int(input("Enter change: Amount of change must be between 0 and 99.\n"))

# set each coin [quarter, dime, nickel, penny] to 0
quarter = 0
dime = 0
nickel = 0
penny = 0

# while loop to test if each addition will equal/overlap user-defined cents
while cents >= 0:
    if cents >= 25:
        quarter += 1
        cents -= 25
    elif cents >= 10:
        dime += 1
        cents -= 10
    elif cents >= 5:
        nickel += 1
        cents -= 5
    elif cents >= 1:
        penny += 1
        cents -= 1
    else:
        print("Amount of change must be between 0 and 99.\n")
        break

# set list
# guessing here: >>> list0 = [quarter, dime, nickel, penny]

# output message displaying appropriate coins to equal user-defined cents
print("Quarters:", quarter)
print("Dimes:", dime)
print("Nickels:", nickel)
print("Pennies:", penny)
Reply


Messages In This Thread
While Loop; Can Else Point Back To Initial Input Command? - by RodNintendeaux - Sep-15-2017, 05:53 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding string numbers, while loop and exit without input. Jose 11 7,578 Apr-15-2020, 08:34 AM
Last Post: Jose
  Question about running comparisons through loop from input value Sunioj 2 2,429 Oct-15-2019, 03:15 PM
Last Post: jefsummers
  Help with loop & user input rdDrp 11 11,535 Dec-23-2016, 06:10 AM
Last Post: rdDrp
  loop in system command fails roadrage 1 3,661 Dec-01-2016, 11:21 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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