How to make a cash change calculaotr?

This is the place for queries that don't fit in any of the other categories.

How to make a cash change calculaotr?

Postby vadimak » Fri May 10, 2013 12:07 am

Hello
how can i make a change calculator?
for exmaple i want to buy an item thet cost 5.89$ and in canada no more 1 cents so how can round the number to 5.90?
vadimak
 
Posts: 15
Joined: Wed May 01, 2013 1:07 pm

Re: How to make a cash change calculaotr?

Postby metulburr » Fri May 10, 2013 12:32 am

Code: Select all
print(round(5.89, 1))
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: How to make a cash change calculaotr?

Postby vadimak » Fri May 10, 2013 1:05 am

The point is i showed only example. the number will be random. and if its 3.32 i need to round this to 3.30. so how can i do it ?
vadimak
 
Posts: 15
Joined: Wed May 01, 2013 1:07 pm

Re: How to make a cash change calculaotr?

Postby metulburr » Fri May 10, 2013 1:31 am

The point is i showed only example. the number will be random. and if its 3.32 i need to round this to 3.30. so how can i do it ?

how about reading some tutorials!

3.3 * 5 is the same as 3.30 * 5, so if you put it in an equation it will be the same. Since you rounded it, the precision is gone anyways. However, if you are just wanting to display it in money format $X.XX, then just use str.format()

Code: Select all
import random

for i in range(10):
    num = random.uniform(1,10)
    rounded = round(num,1)
    print('{} was rounded to {:.2f}'.format(num, rounded))
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: How to make a cash change calculaotr?

Postby Guitarman2010 » Fri May 10, 2013 1:50 am

Thank you Mek......here is my noobish correction of a noobish mistake

Code: Select all
#example1

print 'Enter total:'

total = input()

print '$' + str((round(total,1)))




or with a simple loop and clear statement

Code: Select all

import os

def cls():
  '''Clears any displayed data on screen
    os.system(['clear', 'cls'][os.name == 'nt])
 
again = 1

while again == 1:

    print 'Enter Total:'
    total = input()

    print 'Rounded total $' + str((round(total, 1)))
   
    another = raw_input('Another Go? y/n')

    if another == 'y':
      cls()
      pass
    elif another == 'n':
      comt +=1



I'm still learning also :)
Last edited by Guitarman2010 on Fri May 10, 2013 1:23 pm, edited 3 times in total.
Guitarman2010
 
Posts: 21
Joined: Thu Mar 07, 2013 4:39 pm

Re: How to make a cash change calculaotr?

Postby Mekire » Fri May 10, 2013 4:42 am

Guitarman2010 wrote:Noobish way

Unfortunately wrongish way. Did you try to run it?

Code: Select all
#example

print str('Enter total:') #You don't need to convert a string to a string.

total = input() #You should never use input in python 2.x; use raw_input and convert your result.

#String concatenation in print statements is discouraged, but you have a bigger problem
print str('$') + (round(total,1)) #You converted the str to a str but didn't convert your float; this fails.


-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: How to make a cash change calculaotr?

Postby Kebap » Fri May 10, 2013 8:31 am

Hello vadimak, what happened to your last thread about this idea? Did you follow the advice given there?
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 396
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot] and 2 guests