Jump to content

Simpler way to write this program

- - - - -

  • Please log in to reply
8 replies to this topic

#1
Edu

Edu

    Newbie

  • Members
  • Pip
  • 7 posts
Hey fellas, i am new here, just registered.

I started learning python a couple of days ago from tutorials and some documentation. Haven't really practiced more then 2 hours on it.

My bro employed a new worker a few days ago, as a result of that he needs to count money at the start of day and end of day to see if everything is OK. I wrote a simple program to help him do that, it's nothing really but i was wondering if there's a simpler way to write it.

I will post the code here:

x1 = 5.5

x2 = 8

x3 = 13.5


coins = input("Coins at start of day: ")

cash = input("Cash at start of day: ")


startmoney = coins + cash


x1s = input("Product X1 sold: ")

x1sold = x1s * x1


x2s = input("Product X2 sold: ")

x2sold = x2s * x2


x3s = input("Product X3 sold ")

x3sold = x3s * x3


total_sales = x1sold + x2sold + x3sold


print "Total sales: "

print  total_sales


total_money = total_sales + startmoney

print "Cash should be: "

print total_money


coins_end = input("Coins at the end of day: ")

cash_end = input("Cash at the end of day: ")

endmoney = coins_end + cash_end

print endmoney



Result = endmoney - total_money

print "At the end of the day: "

print Result



raw_input("Press<enter>")



To explain a few things:

Coins - That is spare money or change, i couldn't think of a more appropriate word in english as i translated the code from my native language so that people here can better understand the program.

Cash - well it's cash, paper money.

Startmoney - it's a total of coins and cash that the employee is given at the start of the day.

x1, x2, x3 - are products. They only sell 3 products so i named them like that to be simpler. In the original code every product has it's name.

total_sales - is the total sales made for that day.

total_money - is all the money that SHOULD be in the shop.

end_money - refers to the coins and cash that is found in the store at the end of the day.


I am just curious if i could've written it simpler but since i am a total beginner to python and programming in general(had a couple of days learning of Java a few years back) i couldn't think of a better way to write but i just feel that there's a much simpler way to do it.


Cheers

#2
solartic

solartic

    Learning Programmer

  • Members
  • PipPipPip
  • 95 posts
Welcome to the forum.

product_keys     = ['X1', 'X2', 'X3'] # apparently dictionary data type in python has no concept of order, so this list will determine the order in which the products are printed 
products         = {'X1': 5.5, 'X2': 8, 'X3': 13.5}
total_sales     = 0

coins     = input("Coins at start of day: ")
cash     = input("Cash at start of day: ")

startmoney = coins + cash

for key in product_keys:
    product_count = input("Product " + key + " sold: ")
    total_sales += products[key] * product_count

print "Total sales: %.1f" % total_sales

total_money = total_sales + startmoney
print "Cash should be: %.1f" % total_money

coins_end = input("Coins at the end of day: ")
cash_end = input("Cash at the end of day: ")
endmoney = coins_end + cash_end
print endmoney


Result = endmoney - total_money
print "At the end of the day: %.1f" % Result


raw_input("Press<enter>")

I'm sure there are a few thing that you can do to make your code simpler or rather more readable and maintainable which is a bit more important than simplicity which make the code harder to maintain.

#3
Edu

Edu

    Newbie

  • Members
  • Pip
  • 7 posts
Thanks mate,

I haven't gotten to the for loop yet, it does look useful.

Btw, i was wondering if there is a way to save the results to a file from Python?

i added this to the code but it's not working:

FILE = open ('c:/test/res.txt', 'w')
FILE.write(Result)
FILE.close

I keep getting this error TypeError: expected a character buffer object for the FILE.write(Result) line.

#4
solartic

solartic

    Learning Programmer

  • Members
  • PipPipPip
  • 95 posts
I'm pretty new to python as well and I have not had the need to use file, so I really have no experience there. However base on your error it might be that the write method is expecting a string, and you are passing a float to it. Try and covert the float to a string before trying to save it to the file.

str(Result) or "%f" % Result

#5
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
@solartic: Python 3 is your friend. :)

Quote

 apparently dictionary data type in python has no concept of order, so this list will determine the order in which the products are printed 
import collections


products = collections.OrderedDict([('X1', 5.5), ('X2', 8.0), ('X3', 13.5)])

total_sales = 0.0


for (key, val) in products.items():

    total_sales += val * int(input("Product " + key + " sold: "))

Edited by ZekeDragon, 26 January 2011 - 07:28 PM.

Wow I changed my sig!

#6
solartic

solartic

    Learning Programmer

  • Members
  • PipPipPip
  • 95 posts

ZekeDragon said:

@solartic: Python 3 is your friend. :)


I think your right this is pretty cool thanks

#7
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 890 posts
  • Location:::1
Edu,

you can cast pretty much anything to a string with str() function (it's actually a class constructor). And with print statement, if you don't want to make a new line just put comma after:

print "Line 1"

print "Line 2", "Line 3" # this will be on same line


You can also use + to concatenate strings (+ operator only works for strings while comma works for all types) or you can use C printf() style if you're familiar with it.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#8
Edu

Edu

    Newbie

  • Members
  • Pip
  • 7 posts
Thanks a lot fellas, i couldn't respond earlier due to work. I will try to enhance it a little bit and try different things out, imo that's the best way to learn a language.

I am liking Python a lot so far.

#9
restored

restored

    Newbie

  • Members
  • PipPip
  • 14 posts
a simple list (it's ordered)
[('X1', 5.5), ('X2', 8), ('X3', 13.5)]

convenient init

>>> d = dict(x1=5.5, x2=8, x3=13.5)

>>> d

{'x2': 8, 'x3': 13.5, 'x1': 5.5}

>>>






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users