patternpythonMinor
Tracking hours worked for the days of the week
Viewed 0 times
trackingtheworkedhoursweekfordays
Problem
I am working through a udemy course on Python, this is the first real program I've written. I am new to Python and to programming.
I'd like to replace the
Most relevant code:
```
def main(day):
if day == "Monday":
print "Today is %s, How many hours would you like to add today?" % (day)
hours = int(raw_input())
DOTW['Monday'] = DOTW['Monday'] + hours
print "Cool your hours for %s are %d" %(day, DOTW['Monday'])
want = raw_input("Do you want to add more time? y/n \n > ")
overtime()
leave(want)
if day == "Tuesday":
print "Ah, Today is %s, How many hours have you worked?" %(day)
hours = int(raw_input())
DOTW['Tuesday'] = DOTW['Tuesday'] + hours
print "alright, yours hours for %s are %d" %(day, DOTW['Tuesday'])
want = raw_input("Do you want to add more time? y/n \n > ")
overtime()
leave(want)
if day == "Wednesday":
print "Ah, Today is %s, How many hours have you worked?" %(day)
hours = int(raw_input())
DOTW['Wednesday'] = DOTW['Wednesday'] + hours
print "Cool for %s I've got %d hours" %(day, DOTW['Wednesday'])
want = raw_input("Do you want to add more time? y/n \n > ")
overtime()
leave(want)
if day == "Thursday":
print "Ah, Today is %s, How many hours have you worked?" %(day)
hours = int(raw_input())
DOTW['Thursday'] = DOTW['Thursday'] + hours
print "Your hours for %s are %d" %(day, DOTW['Thursday'])
want = raw_input("Do you want to add more time? y/n \n > ")
overtime()
leave(want)
if day == "Friday":
print "FI
I'd like to replace the
main function below with something more efficient and less repetitive but I'm not sure what or how. The most I could think to do is write a function for adding hours and call it under each day.Most relevant code:
```
def main(day):
if day == "Monday":
print "Today is %s, How many hours would you like to add today?" % (day)
hours = int(raw_input())
DOTW['Monday'] = DOTW['Monday'] + hours
print "Cool your hours for %s are %d" %(day, DOTW['Monday'])
want = raw_input("Do you want to add more time? y/n \n > ")
overtime()
leave(want)
if day == "Tuesday":
print "Ah, Today is %s, How many hours have you worked?" %(day)
hours = int(raw_input())
DOTW['Tuesday'] = DOTW['Tuesday'] + hours
print "alright, yours hours for %s are %d" %(day, DOTW['Tuesday'])
want = raw_input("Do you want to add more time? y/n \n > ")
overtime()
leave(want)
if day == "Wednesday":
print "Ah, Today is %s, How many hours have you worked?" %(day)
hours = int(raw_input())
DOTW['Wednesday'] = DOTW['Wednesday'] + hours
print "Cool for %s I've got %d hours" %(day, DOTW['Wednesday'])
want = raw_input("Do you want to add more time? y/n \n > ")
overtime()
leave(want)
if day == "Thursday":
print "Ah, Today is %s, How many hours have you worked?" %(day)
hours = int(raw_input())
DOTW['Thursday'] = DOTW['Thursday'] + hours
print "Your hours for %s are %d" %(day, DOTW['Thursday'])
want = raw_input("Do you want to add more time? y/n \n > ")
overtime()
leave(want)
if day == "Friday":
print "FI
Solution
The first step to reducing repetition is to use variables instead of literals. Why call
The only difference between days is in the messages that you print to the user. You've given custom messages, but you could just put those in a dictionary and access them the same way you access
With those changes you could make it all one block with just one test because Friday's message is an exception that doesn't need the string formatting argument.
Also you should use the newer
The particular advantage for you here is that formatting a message that takes no arguments won't raise errors, unlike with
This means you no longer need to test for
DOTW['Monday'] when you could just use day as your key. Importantly, this will avoid errors like the one you created in the Sunday block, where you actually display Monday's hours instead of Sunday's.The only difference between days is in the messages that you print to the user. You've given custom messages, but you could just put those in a dictionary and access them the same way you access
DOTW. Here's an example of how the greeting could look:today_messages = {
"Monday": "Today is %s, How many hours would you like to add today?",
"Tuesday": "Ah, Today is %s, How many hours have you worked?",
"Wednesday": "Ah, Today is %s, How many hours have you worked?",
"Thursday": "Ah, Today is %s, How many hours have you worked?",
"Friday": "FINALLY IT'S FRIDAY!!! How many hours have you worked?",
"Saturday": "Ah, Today is %s, How many hours have you worked?",
"Sunday": "Ah, Today is %s, How many hours have you worked?",
}With those changes you could make it all one block with just one test because Friday's message is an exception that doesn't need the string formatting argument.
def main(day):
if day == "Friday":
print today_messages[day]
else:
print today_messages[day] % day
hours = int(raw_input())
DOTW[day] = DOTW[day] + hours
print hours_worked[day] %(day, DOTW[day])
want = raw_input("Do you want to add more time? y/n \n > ")
overtime()
leave(want)Also you should use the newer
str.format method. Using % is the old way, and str.format has a lot of useful formatting options. You don't particularly need them in your case but it's clearer and cleaner syntax. The basic way to use it is like this:"Sunday": "Ah, Today is {}, How many hours have you worked?",
...
print today_messages[day].format(day)The particular advantage for you here is that formatting a message that takes no arguments won't raise errors, unlike with
%. >>> string = "Ignore me"
>>> "Hello World!" % string
Traceback (most recent call last):
File "", line 1, in
"Hello World!" % string
TypeError: not all arguments converted during string formatting
>>> "Hello World!".format(string)
'Hello World!'This means you no longer need to test for
"Friday", since the format call will just be ignore for that string:def main(day):
print today_messages[day].format(day)
hours = int(raw_input())
DOTW[day] = DOTW[day] + hours
print hours_worked[day].format(day, DOTW[day])
want = raw_input("Do you want to add more time? y/n \n > ")
overtime()
leave(want)Code Snippets
today_messages = {
"Monday": "Today is %s, How many hours would you like to add today?",
"Tuesday": "Ah, Today is %s, How many hours have you worked?",
"Wednesday": "Ah, Today is %s, How many hours have you worked?",
"Thursday": "Ah, Today is %s, How many hours have you worked?",
"Friday": "FINALLY IT'S FRIDAY!!! How many hours have you worked?",
"Saturday": "Ah, Today is %s, How many hours have you worked?",
"Sunday": "Ah, Today is %s, How many hours have you worked?",
}def main(day):
if day == "Friday":
print today_messages[day]
else:
print today_messages[day] % day
hours = int(raw_input())
DOTW[day] = DOTW[day] + hours
print hours_worked[day] %(day, DOTW[day])
want = raw_input("Do you want to add more time? y/n \n > ")
overtime()
leave(want)"Sunday": "Ah, Today is {}, How many hours have you worked?",
...
print today_messages[day].format(day)>>> string = "Ignore me"
>>> "Hello World!" % string
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
"Hello World!" % string
TypeError: not all arguments converted during string formatting
>>> "Hello World!".format(string)
'Hello World!'def main(day):
print today_messages[day].format(day)
hours = int(raw_input())
DOTW[day] = DOTW[day] + hours
print hours_worked[day].format(day, DOTW[day])
want = raw_input("Do you want to add more time? y/n \n > ")
overtime()
leave(want)Context
StackExchange Code Review Q#112288, answer score: 4
Revisions (0)
No revisions yet.