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?
#8
I find it a little easier to use zip on the two lists in the for loop to avoid indexing the lists.

(Also, changed / to // as OP appears to be using Python 3, and simplified the answer string generation)

currency_values = [25, 10, 5, 1]
currency_names = ['quarter', '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 zip(currency_names, 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 +=  f' {current_currency_number} {name} '
        cents_to_convert = cents_to_convert % currency
print(final_answer)

Couldn't resist (I'm bored) tweaking, and demonstrating the point @Krookroo made about how easy it is to change currency when using two lists:

#!/usr/bin/env python3
# simple programme to calculate largest denominations of change to give

# denominations lists, values and names one-to-one mapping!
# sterling
currency_values = [200, 100, 50, 20, 10, 5, 2, 1]
currency_names = ['two pound', 'pound', 'fifty pence', 
                    'twenty pence', 'ten pence', 
                    'five pence', 'two pence', 'one pence']

def breakdown(amountstr):
  if not amountstr:
    return ""
  change = ""
  amount = int(amountstr)
  for name, currency in zip(currency_names, currency_values):
      if amount <= 0: # end if no change left
          break
      if amount >= currency: # we have some of this denomination to pay
          quantity = amount // currency
          change +=  f'\n{quantity}x \t{name} '
          amount = amount % currency
  return change
 
while True:
  change = breakdown(input('\nHow much needs to be returned in change (return to exit)? '))
  if change:
    print(f'Change breakdown is: {change}')
  else:
    break
Also available on repl.it.
I am trying to help you, really, even if it doesn't always seem that way
Reply


Messages In This Thread
RE: While Loop; Can Else Point Back To Initial Input Command? - by gruntfutuk - Oct-01-2017, 05:18 PM

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,533 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