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?
#6
Nice :)
Just add a little bit, here's how I would have done it. The idea is to store the data we use in lists so that we can very easily tweak the algorithm for another set of currencies, just by editing the lists containing the currencies and their values. The logic behind is the same as yours, expressed in a more general way.
There might be a cleaner / more efficient way to do this with dictionaries, but I never used dictionaries before (and won't for a while).

currency_values = [25, 10, 5, 1]
currency_names = ['quartet', 'dime', 'nickel', 'cent']

cents_to_convert = int(input('Hey little boy how much cents do you want me to convert?\n'))

final_answer = "I'll give you "
for name, currency in enumerate(currency_values):
    if cents_to_convert<=0: #if we have converted all the cents, just end and give the result.
        break
    if cents_to_convert>=currency: #if less, we don't need to print that we give this guy 0 of this coin.
        current_currency_number = cents_to_convert/currency
        final_answer += ' {0} {1} '.format(str(current_currency_number), currency_names[name])
        cents_to_convert = cents_to_convert % currency
print final_answer
Reply


Messages In This Thread
RE: While Loop; Can Else Point Back To Initial Input Command? - by Krookroo - Sep-17-2017, 06:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding string numbers, while loop and exit without input. Jose 11 7,580 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,536 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