473,396 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Declaration of global variable????

Since I am pretty new to Python, I am not sure what am I doing wrong here. I am getting error when I compile this program at module "def printResults()" claming that I am using variables prior to global declaration. I have put bold for the variables "custName and serviceCharge" in the module. Any help will be appreciated greatly.

Thanks a bunch in advance!!!!

Andy




Expand|Select|Wrap|Line Numbers
  1. def getInput():
  2.     global custName, checkAmount
  3.     custName = char (raw_input("Enter customer name: "))
  4.     gateDates()
  5.     checkAmount = int (raw_input("Enter check amount: "))
  6.  
  7. def getDates():
  8.     global checkYear, checkMonth, checkDay, todayYear, todayMonth, todayDay
  9.     print "Enter the date of the check\n"
  10.     checkYear = int (raw_input ("Enter the check year: "))
  11.     checkMonth = int (raw_input ("Enter the check month: "))
  12.     checkDay = int (raw_input ("Enter the check date: "))
  13.     print "Enter today's date\n"
  14.     todayYear = int (raw_input ("Enter the today's year: "))
  15.     todayMonth = int (raw_input ("Enter the today's month: "))
  16.     todayDay = int (raw_input ("Enter the today's date: "))
  17.  
  18. def calculateServiceCharge():
  19.     global baseCahrge, extraCharge, checkAmount, yearsLate, todayYear, checkYear
  20.     global todayWorkField, todayMonth, monthsLate, checkMonth, serviceCharge
  21.     baseCharge = 20.00
  22.     extraCharge = 0.02 * checkAmount
  23.     yearsLate = todayYear - checkYear
  24.     todayWorkField = yearsLate * 12 + todayMonth
  25.     monthsLate = todayWorkField - checkMonth
  26.     serviceCharge = baseCharge + extraCharge + monthsLate * 5
  27.  
  28.  
  29. def printResults():
  30.     global serviceCharge, custName, baseCahrge, extraCharge, checkAmount, yearsLate, todayYear, checkYear
  31.     global todayWorkField, todayMonth, monthsLate, checkMonth
  32.     print "Customer Name  : ",+custName
  33.     print "Service Charge : ",+serviceCharge
  34.  
  35.  
  36. #Main------------------------------------------------------------
  37.     global custName, checkYear, checkMonth, checkDay, todayYear, todayMonth, todayDay
  38.     global checkAmonut, serviceCharge, extraCharge, yearsLate, monthsLate, todayWorkField
  39.  
  40.     getInput()
  41.     calculateServiceCharge()
  42.     printResults()
Dec 4 '06 #1
3 3324
bvdet
2,851 Expert Mod 2GB
Since I am pretty new to Python, I am not sure what am I doing wrong here. I am getting error when I compile this program at module "def printResults()" claming that I am using variables prior to global declaration. I have put bold for the variables "custName and serviceCharge" in the module. Any help will be appreciated greatly.

Thanks a bunch in advance!!!!

Andy



Expand|Select|Wrap|Line Numbers
  1.  
  2. def getInput():
  3.     global custName, checkAmount
  4.     custName = char (raw_input("Enter customer name: "))
  5.     gateDates()
  6.     checkAmount = int (raw_input("Enter check amount: "))
  7.  
  8. def getDates():
  9.     global checkYear, checkMonth, checkDay, todayYear, todayMonth, todayDay
  10.     print "Enter the date of the check\n"
  11.     checkYear = int (raw_input ("Enter the check year: "))
  12.     checkMonth = int (raw_input ("Enter the check month: "))
  13.     checkDay = int (raw_input ("Enter the check date: "))
  14.     print "Enter today's date\n"
  15.     todayYear = int (raw_input ("Enter the today's year: "))
  16.     todayMonth = int (raw_input ("Enter the today's month: "))
  17.     todayDay = int (raw_input ("Enter the today's date: "))
  18.  
  19. def calculateServiceCharge():
  20.     global baseCahrge, extraCharge, checkAmount, yearsLate, todayYear, checkYear
  21.     global todayWorkField, todayMonth, monthsLate, checkMonth, serviceCharge
  22.     baseCharge = 20.00
  23.     extraCharge = 0.02 * checkAmount
  24.     yearsLate = todayYear - checkYear
  25.     todayWorkField = yearsLate * 12 + todayMonth
  26.     monthsLate = todayWorkField - checkMonth
  27.     serviceCharge = baseCharge + extraCharge + monthsLate * 5
  28.  
  29.  
  30. def printResults():
  31.     global serviceCharge, custName, baseCahrge, extraCharge, checkAmount, yearsLate, todayYear, checkYear
  32.     global todayWorkField, todayMonth, monthsLate, checkMonth
  33.     print "Customer Name  : ",+custName
  34.     print "Service Charge : ",+serviceCharge
  35.  
  36.  
  37. #Main------------------------------------------------------------
  38.     global custName, checkYear, checkMonth, checkDay, todayYear, todayMonth, todayDay
  39.     global checkAmonut, serviceCharge, extraCharge, yearsLate, monthsLate, todayWorkField
  40.  
  41.     getInput()
  42.     calculateServiceCharge()
  43.     printResults()
  44.  
  45.  
There is no built-in function 'char'. The built-in function 'chr' requires an integer.
Look for typos: baseCahrge, gateDates()

Personally I don't like to pass around global variables. Instead I would pass values to and from the functions as required. You can also read the current date with time.localtime and do 'date' math.
Expand|Select|Wrap|Line Numbers
  1. def getInput():
  2.     cn = (raw_input("Enter customer name: "))
  3.     ca = float(raw_input("Enter check amount: "))
  4.     return cn, ca
  5.  
  6. def getDates():
  7.     cd = raw_input ("Enter the date of the check yyyy/mm/dd")
  8.     date_list = cd.split('/')
  9.     cd1 = date(int(date_list[0]), int(date_list[1]), int(date_list[2]))
  10.     td = date(time.localtime()[0], time.localtime()[1], time.localtime()[2])
  11.     return cd1, td
  12.  
  13. def calculateServiceCharge(checkDate, todayDate, checkAmount):
  14.     # Assume service charge of $0.17 per day
  15.     baseCharge = 20.00
  16.     extraCharge = 0.02 * checkAmount
  17.     return baseCharge + extraCharge + ((todayDate-checkDate).days * 0.17)
  18.  
  19.  
  20. def printResults(custName, serviceCharge):
  21.     print "Customer Name: %s\nService Charge: $%0.2f" % (custName, serviceCharge)
  22.  
  23. #Main------------------------------------------------------------
  24.  
  25. custName, checkAmount = getInput()
  26. checkDate, todayDate = getDates()
  27. printResults(custName, calculateServiceCharge(checkDate, todayDate, checkAmount))
The variable names local to the functions need not match the global variable names assigned outside the functions.
Dec 4 '06 #2
Thanks bvdet for your reply.

Andy
Dec 4 '06 #3
bvdet
2,851 Expert Mod 2GB
You are welcome Andy!
Dec 4 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Joseph George | last post by:
How do I hold a global variable in VB.NET? I am creating as ASP web form like this: Public Class WebForm1 Inherits System.Web.UI.Page Dim list as new ListItemCollection() - - - Private Sub...
2
by: francisl | last post by:
I have a problem when I declare global variable. If it is string, dict, array or tuple, everything goes fine, but with int, I get an "used before declaration" error. here a sample : vardict =...
3
by: Eric Lilja | last post by:
Hello, I have a few global variables in my program. One of them holds the name of the application and it's defined in a header file globals.hpp (and the point of definition also happen to be the...
2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
7
by: Method Man | last post by:
Can someone explain the scope/linkage differences between the following 4 global declarations and when one should be used (theoretically) over the rest? sample.h --------- #ifndef SAMPLE_H...
19
by: moxm | last post by:
I have a statement declares a globle variable like this : char *pname = NULL; Then I used splint to check the code, I got errors: err.c:8:15: Global pname initialized to null value: pname =...
7
by: g | last post by:
hello! I wanna use a global variable,witch is the best way(I don't want to use a Singleton for an integer)? I try : #ifndef DUMY_H_ #define DUMY_H_ int connections; #endif /*DUMY_H_*/
10
by: subramanian100in | last post by:
Suppose I declare a global variable int g; in two different files say a.c which has main() and b.c When I compile them to build an executable under gcc in Redhat Linux with the command ...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
20
by: teddysnips | last post by:
Weird. I have taken over responsibility for a legacy application, Access 2k3, split FE/BE. The client has reported a problem and I'm investigating. I didn't write the application. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.