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 - def getInput():
-
global custName, checkAmount
-
custName = char (raw_input("Enter customer name: "))
-
gateDates()
-
checkAmount = int (raw_input("Enter check amount: "))
-
-
def getDates():
-
global checkYear, checkMonth, checkDay, todayYear, todayMonth, todayDay
-
print "Enter the date of the check\n"
-
checkYear = int (raw_input ("Enter the check year: "))
-
checkMonth = int (raw_input ("Enter the check month: "))
-
checkDay = int (raw_input ("Enter the check date: "))
-
print "Enter today's date\n"
-
todayYear = int (raw_input ("Enter the today's year: "))
-
todayMonth = int (raw_input ("Enter the today's month: "))
-
todayDay = int (raw_input ("Enter the today's date: "))
-
-
def calculateServiceCharge():
-
global baseCahrge, extraCharge, checkAmount, yearsLate, todayYear, checkYear
-
global todayWorkField, todayMonth, monthsLate, checkMonth, serviceCharge
-
baseCharge = 20.00
-
extraCharge = 0.02 * checkAmount
-
yearsLate = todayYear - checkYear
-
todayWorkField = yearsLate * 12 + todayMonth
-
monthsLate = todayWorkField - checkMonth
-
serviceCharge = baseCharge + extraCharge + monthsLate * 5
-
-
-
def printResults():
-
global serviceCharge, custName, baseCahrge, extraCharge, checkAmount, yearsLate, todayYear, checkYear
-
global todayWorkField, todayMonth, monthsLate, checkMonth
-
print "Customer Name : ",+custName
-
print "Service Charge : ",+serviceCharge
-
-
-
#Main------------------------------------------------------------
-
global custName, checkYear, checkMonth, checkDay, todayYear, todayMonth, todayDay
-
global checkAmonut, serviceCharge, extraCharge, yearsLate, monthsLate, todayWorkField
-
-
getInput()
-
calculateServiceCharge()
-
printResults()
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 -
-
def getInput():
-
global custName, checkAmount
-
custName = char (raw_input("Enter customer name: "))
-
gateDates()
-
checkAmount = int (raw_input("Enter check amount: "))
-
-
def getDates():
-
global checkYear, checkMonth, checkDay, todayYear, todayMonth, todayDay
-
print "Enter the date of the check\n"
-
checkYear = int (raw_input ("Enter the check year: "))
-
checkMonth = int (raw_input ("Enter the check month: "))
-
checkDay = int (raw_input ("Enter the check date: "))
-
print "Enter today's date\n"
-
todayYear = int (raw_input ("Enter the today's year: "))
-
todayMonth = int (raw_input ("Enter the today's month: "))
-
todayDay = int (raw_input ("Enter the today's date: "))
-
-
def calculateServiceCharge():
-
global baseCahrge, extraCharge, checkAmount, yearsLate, todayYear, checkYear
-
global todayWorkField, todayMonth, monthsLate, checkMonth, serviceCharge
-
baseCharge = 20.00
-
extraCharge = 0.02 * checkAmount
-
yearsLate = todayYear - checkYear
-
todayWorkField = yearsLate * 12 + todayMonth
-
monthsLate = todayWorkField - checkMonth
-
serviceCharge = baseCharge + extraCharge + monthsLate * 5
-
-
-
def printResults():
-
global serviceCharge, custName, baseCahrge, extraCharge, checkAmount, yearsLate, todayYear, checkYear
-
global todayWorkField, todayMonth, monthsLate, checkMonth
-
print "Customer Name : ",+custName
-
print "Service Charge : ",+serviceCharge
-
-
-
#Main------------------------------------------------------------
-
global custName, checkYear, checkMonth, checkDay, todayYear, todayMonth, todayDay
-
global checkAmonut, serviceCharge, extraCharge, yearsLate, monthsLate, todayWorkField
-
-
getInput()
-
calculateServiceCharge()
-
printResults()
-
-
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. - def getInput():
-
cn = (raw_input("Enter customer name: "))
-
ca = float(raw_input("Enter check amount: "))
-
return cn, ca
-
-
def getDates():
-
cd = raw_input ("Enter the date of the check yyyy/mm/dd")
-
date_list = cd.split('/')
-
cd1 = date(int(date_list[0]), int(date_list[1]), int(date_list[2]))
-
td = date(time.localtime()[0], time.localtime()[1], time.localtime()[2])
-
return cd1, td
-
-
def calculateServiceCharge(checkDate, todayDate, checkAmount):
-
# Assume service charge of $0.17 per day
-
baseCharge = 20.00
-
extraCharge = 0.02 * checkAmount
-
return baseCharge + extraCharge + ((todayDate-checkDate).days * 0.17)
-
-
-
def printResults(custName, serviceCharge):
-
print "Customer Name: %s\nService Charge: $%0.2f" % (custName, serviceCharge)
-
-
#Main------------------------------------------------------------
-
-
custName, checkAmount = getInput()
-
checkDate, todayDate = getDates()
-
printResults(custName, calculateServiceCharge(checkDate, todayDate, checkAmount))
The variable names local to the functions need not match the global variable names assigned outside the functions.
Thanks bvdet for your reply.
Andy
bvdet 2,851
Expert Mod 2GB Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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 =...
|
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...
|
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
{...
|
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...
|
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 =...
|
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_*/
|
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
...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
| |