8 Replies - 430 Views - Last Post: 25 January 2019 - 05:37 PM Rate Topic: -----

#1 gneisler   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 23-September 11

I am new to python and can not get this change calculattor to function

Posted 20 January 2019 - 10:18 AM

#!/usr/bin/evn python3
#
# Change Calculator Program
# The program will display minimun number of coins
#
print ("Change Calclator")
print ()

quarters = 25
dimes = 10
nickels = 5
pennies = 1
 
# get choice from the user
choice = "y"
while choice.lower() == "y":
    
    # get input from the user
    # times 100 to convert dollard to pennies
    dollar_amount = (input("Enter dollar amount( for example, .65, 3.95):")*100)
    print()

    quarters = (dollar_amount //25)
    dimes   =  dollar_amount % 25 // 10
    nickels =  dollar_amount % 25% 10//5
    pennies =  dollar_amount % 25% 10 % 5 // 1

    # Display the results
    print("Quarters:", quarters)
    print("Dimes:", dimes)
    print("Nickels:", nickels)
    print("Pennies:", pennies)
    print()

    # see if the user wants to continue
    choice = input("Continue (y/n)?: ")
    print ()

print ("Bye!")


Is This A Good Question/Topic? 0
  • +

Replies To: I am new to python and can not get this change calculattor to function

#2 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 507
  • View blog
  • Posts: 1,598
  • Joined: 27-December 13

Re: I am new to python and can not get this change calculattor to function

Posted 20 January 2019 - 11:27 AM

input() returns a string.
You can convert to float like this:
dollar_amount = float(input("Enter dollar amount( for example, .65, 3.95):")) * 100

Was This Post Helpful? 0
  • +
  • -

#3 gneisler   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 23-September 11

Re: I am new to python and can not get this change calculattor to function

Posted 20 January 2019 - 01:57 PM

Thank You, for all your help.
Was This Post Helpful? 0
  • +
  • -

#4 gneisler   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 23-September 11

Re: I am new to python and can not get this change calculattor to function

Posted 21 January 2019 - 05:59 PM

How do I go about setting up a display error message if the user enters, say z, instead of y or x?
#!/usr/bin/evn python3
#
# Change Calculator Program
# The program will display minimun number of coins
#
print ("Change Calclator")
print ()

quarters = 25
dimes   =  10
nickels =  5
pennies =  1

 
# get choice from the user
choice = "y"

while choice.lower() == "y":
    
    # get input from the user
    # times 100 to convert dollard to pennies
    dollar_amount = float(input("Enter dollar amount( for example, .65, 3.95): ")) * 100
    print()

    quarters =  int(dollar_amount // 25)
    dimes   =   int(dollar_amount % 25 // 10)
    nickels =   int(dollar_amount % 25 % 10//5)
    pennies =   int(dollar_amount % 25% 10 % 5 // 1)

    # Display the results
    print("Quarters:", quarters)
    print("Dimes:   ",  dimes)
    print("Nickels: ",  nickels)
    print("Pennies: ",  pennies)
    print()

    # see if the user wants to continue
    choice = input("Continue (y/n)?: ")
    print ()


Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is online

  • Stealth IT
  • member icon

Reputation: 6739
  • View blog
  • Posts: 27,749
  • Joined: 12-December 12

Re: I am new to python and can not get this change calculattor to function

Posted 22 January 2019 - 01:51 AM

You are storing their input in variable 'choice', you could introduce an if statement to check what they entered. Give it a go.
Was This Post Helpful? 0
  • +
  • -

#6 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 507
  • View blog
  • Posts: 1,598
  • Joined: 27-December 13

Re: I am new to python and can not get this change calculattor to function

Posted 22 January 2019 - 03:25 AM

Are you familiar with functions?
If not, this is a good occasion to introduce yourself to this subject.
Take a good look at the first code in this tutorial: https://www.dreaminc...and-game-loops/

The idea is to move all the user input handling to a separate function.
This function will return either 'True' or 'False' (meaning continue or stop).
Your while loop criteria should then just check this return value.

As andrewsw siad: Give it a try..
Was This Post Helpful? 0
  • +
  • -

#7 gneisler   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 23-September 11

Re: I am new to python and can not get this change calculattor to function

Posted 25 January 2019 - 02:30 PM

I am kind of stuck, with setting up the yes or no loop.
#!/usr/bin/evn python3
#
# Change Calculator Program
# The program will display minimun number of coins
#
print ("Change Calclator")
print ()

quarters = 25
dimes   =  10
nickels =  5
pennies =  1
 


choice = "y"
choice = "n"

# get input from the user
# times 100 to convert dollard to pennies
      
while choice.lower() == "y":            
   dollar_amount = float(input("Enter dollar amount( for example, .65, 3.95): ")) * 100
    print()

    quarters =  int(dollar_amount // 25)
    dimes   =   int(dollar_amount % 25 // 10)
    nickels =   int(dollar_amount % 25 % 10//5)
    pennies =   int(dollar_amount % 25% 10 % 5 // 1)

    # Display the results
    print("Quarters:", quarters)
    print("Dimes:   ",  dimes)
    print("Nickels: ",  nickels)
    print("Pennies: ",  pennies)
    print()

          
   if choice.lower() == "y":
   choice = input("Continue (y/n)?: ")
   print()    
            
   elif choice.lower() == "n":
            
   print("GoodBye!")
   break
   else: print("Please enter y/n, try again.")
   print ()



Was This Post Helpful? 0
  • +
  • -

#8 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 507
  • View blog
  • Posts: 1,598
  • Joined: 27-December 13

Re: I am new to python and can not get this change calculattor to function

Posted 25 January 2019 - 04:17 PM

ok, let me help this much. Your code can have this structure:
#!/usr/bin/evn python3
#
# Change Calculator Program
# The program will display minimun number of coins
#
print ("Change Calclator")
print ()

quarters = 25
dimes   =  10
nickels =  5
pennies =  1

def run_again():
    """
    this is a function for handling the user input
    """
    
    result = None

    # some code that makes 'result' take the value
    # 'True' or 'False',
    # depending on the user input...

    return result


go_on = True  # 'go_on' is a new variable controlling your main loop
      
while go_on:  # same as 'while go_on is True:', - but simpler to write...       

    # your existing code here
          
    go_on = run_again()  # assign the output from 'run_again' to 'go_on'



If you combine with the tutorial, I'd expect you to succeed.
Let us know..
Was This Post Helpful? 0
  • +
  • -

#9 gneisler   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 23-September 11

Re: I am new to python and can not get this change calculattor to function

Posted 25 January 2019 - 05:37 PM

Thank You, for your much-needed help. I should be able to get it up and working.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1