# Python code Review

**URL:** <https://forum.freecodecamp.org/t/python-code-review/427129>\
**Category:** I Built This\
**Created:** [October 25, 2020, 8:43pm UTC](https://forum.freecodecamp.org/t/python-code-review/427129 "2020-10-25T20:43:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mootarek](https://avatars.discourse-cdn.com/v4/letter/m/9de053/32.png) [@mootarek](https://forum.freecodecamp.org/u/mootarek)\
**Post date:** [October 25, 2020, 8:43pm UTC](https://forum.freecodecamp.org/t/python-code-review/427129/1 "2020-10-25T20:43:30Z")

</div>

Hello Folks,

I wrote this basic python program which is a time calculator

[https://repl.it/@mootarek/timecalculator](https://repl.it/@mootarek/timecalculator)

In this python challenge:

[https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/time-calculator[https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/time-calculator]](https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/time-calculator%5Bhttps://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/time-calculator%5D)

I started learning python recently so I am not good at it, but I knew other languages before it, so It was kinda easy.

I am asking that some python fluent review this code (link above in the repl) and tell me what is bad about it, how can I improve my code in python as I am planning to code a big python project, also tell me the good parts in the code (if any) but I think it’s a bit modular bec. there’s a separate function for nearly anything.

Thanks, Guys

---

<div class="post-metadata">

**Author:** ![mukeshgurpude](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mukeshgurpude/32/180919_2.png) [@mukeshgurpude](https://forum.freecodecamp.org/u/mukeshgurpude)\
**Post date:** [October 26, 2020, 6:45am UTC](https://forum.freecodecamp.org/t/python-code-review/427129/2 "2020-10-26T06:45:11Z")

</div>

You are using lot of short function which are not reused also…  
You should review your logic in some cases

- ![image](https://global.discourse-cdn.com/freecodecamp/original/3X/e/a/ea3bc00deadc34d93a09a38985585f07b57ef967.png)  
You can do above thing simply by `num%7`, and no need of function
- ![image](https://global.discourse-cdn.com/freecodecamp/original/3X/e/6/e63b43558ed2488958afbdc9d96ac11277dec84e.png)

You can implement above thing by using `if elif` only

- ![image](https://global.discourse-cdn.com/freecodecamp/original/3X/b/c/bc8fbd2220b59703234939160bd70c0ed52fdd28.png)

use [rjust](https://www.w3schools.com/python/ref_string_rjust.asp) function for implementing this

- ![image](https://global.discourse-cdn.com/freecodecamp/original/3X/8/2/826f49b4cbd461f0741cd697c3a1910cdb558beb.png)

this `if` is unnecessary as, `form` is either “AM” or “PM”., condition is always true, hence unnecessary comparison…

> I know you’ve put lot of efforts and tried to solve without searching for additional help, by implementing your thinking as it is. But you should always follow best practices and try to reduce execution time.  
> Try to reduce use of loops and additional unnecessary variables wherever possible.

Good job, you’ve completed your assignment, but now work on make it more efficient 🙂 👍

---

<div class="post-metadata">

**Author:** ![mootarek](https://avatars.discourse-cdn.com/v4/letter/m/9de053/32.png) [@mootarek](https://forum.freecodecamp.org/u/mootarek)\
**Post date:** [October 27, 2020, 5:53am UTC](https://forum.freecodecamp.org/t/python-code-review/427129/3 "2020-10-27T05:53:34Z")

</div>

Thanks a lot for this detailed review. yes, I think the code needs to be Improved. It’s a basic program that just outputs the time after some addition, and I made it unreadable and inefficient. I’ll work on making it cleaner and will also reuse some code.

---

<div class="post-metadata">

**Author:** ![mootarek](https://avatars.discourse-cdn.com/v4/letter/m/9de053/32.png) [@mootarek](https://forum.freecodecamp.org/u/mootarek)\
**Post date:** [October 27, 2020, 7:57am UTC](https://forum.freecodecamp.org/t/python-code-review/427129/4 "2020-10-27T07:57:08Z")

</div>

I think this is better no aimless pure functions bouncing here and there:

```auto
listofdays = [
    "sunday",
    "monday",
    "tuesday",  
    "wednesday", 
    "thursday", # 6 + 12 = 18 formula
    "friday",       
    "saturday", 
]

def get_days(tothrs, day, form):    
    numdays = 0
    dayreturned = ''
    if day == '': 
        while tothrs >= 24:
          tothrs = tothrs - 24
          numdays = numdays + 1 

        numdays = (numdays + 1 if tothrs >= 12 and form == 'PM'
        else numdays)

        if numdays == 1:
            dayreturned = "next day"
        elif numdays > 1:
            dayreturned = f"{numdays} days later"  
        return tothrs if tothrs != 0 else 12, dayreturned
    else:
        dayreturned = ''
        indexofday = listofdays.index(day)

        while tothrs >= 24:
          tothrs = tothrs - 24
          numdays = numdays + 1

        numdays = (numdays + 1 if tothrs >= 12 and form == 'PM' 
        else numdays)

        if (numdays + indexofday) >= len(listofdays) :
            # number = len(listofdays) + numdays + indexofday
            temp = numdays
            numdays = numdays % 7
            dayreturned = (listofdays[numdays] + ' next day' 
            if temp == 1 
            else listofdays[numdays] + f" ({temp} days later)")

        elif numdays == 0 :
            dayreturned = day

        elif numdays == 1 :
            dayreturned = (listofdays[numdays + indexofday] 
            + " next day")
        else:
            dayreturned = (listofdays[numdays + indexofday] + 
            f" ({numdays} days later)") 

        return tothrs if tothrs != 0 else 12, dayreturned   

def add_time(start, duration, day=''):
  starting = start.split(" ")
  num = starting[0]
  form = starting[1]
  starttime = num.split(':')
  hours = starttime[0]
  mins = starttime[1]
  addedtime = duration.split(':')
  addedhrs = addedtime[0]
  addedmins = addedtime[1]
  tothrs = int(hours) + int(addedhrs)
  totmins = int(mins) + int(addedmins) 
  returnedstr = ''

  # incrementing hrs if totalmins exceed 60
  if(totmins > 60):
     tothrs += 1
     totmins = totmins - 60

  tothrs, returnedstr = get_days(int(tothrs), day.lower(), form)  

# getting the right form
  if form == 'AM':
    if tothrs >= 12:
      form = 'PM'
  elif form == 'PM':
    if tothrs >= 12:
      form = "AM"

# getting the right hrs
  if tothrs > 12:
       tothrs = tothrs - 12 

# getting the right mins format
  if totmins < 10:
      totmins = '0' + str(totmins)    

# final results
  if returnedstr != '': 
      print("{0}:{1} {2}, {3}".format(tothrs, totmins, form, returnedstr))
  else:
      print("{0}:{1}, {2}".format(tothrs, totmins, form, returnedstr))

```

Thanks a lot, @mukeshgurpude.

check out this see if it lacks something 🙃 🙃

---

<div class="post-metadata">

**Author:** ![mukeshgurpude](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mukeshgurpude/32/180919_2.png) [@mukeshgurpude](https://forum.freecodecamp.org/u/mukeshgurpude)\
**Post date:** [October 27, 2020, 11:18am UTC](https://forum.freecodecamp.org/t/python-code-review/427129/5 "2020-10-27T11:18:16Z")

</div>

Dear @mootarek,  
You’ve made your code much more readable and efficient.  
For further optimization I would suggest use of modulo(`%`) operator and string formatting methods such as `rjust`, `ljust`.  
I can’t assure you that this are best practices, they’ll reduce conditional statements in your code.  
For example

> [@mootarek](#):
>
> ```auto
> if(totmins > 60):
> tothrs += 1
> totmins = totmins - 60
> 
> ```

You can do this with

```py
tothrs += totmins//60
totmin %= 60

```

> [@mootarek](#):
>
> ```auto
> if totmins < 10:
> totmins = '0' + str(totmins)
> 
> ```

This can be achieved with `str(totmins).rjust(2, '0')`

> I just want to say you that keep this attitude of making code efficient after finishing it. Most of the people leave the project as soon as they get the result without worrying about worse cases of input.  
> So keep it up 👍

---

<div class="post-metadata">

**Author:** ![mootarek](https://avatars.discourse-cdn.com/v4/letter/m/9de053/32.png) [@mootarek](https://forum.freecodecamp.org/u/mootarek)\
**Post date:** [October 28, 2020, 1:07pm UTC](https://forum.freecodecamp.org/t/python-code-review/427129/6 "2020-10-28T13:07:57Z")

</div>

We can collaborate on a project if you have some time 😉

---

<div class="post-metadata">

**Author:** ![mukeshgurpude](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mukeshgurpude/32/180919_2.png) [@mukeshgurpude](https://forum.freecodecamp.org/u/mukeshgurpude)\
**Post date:** [October 28, 2020, 1:56pm UTC](https://forum.freecodecamp.org/t/python-code-review/427129/7 "2020-10-28T13:56:11Z")

</div>

What type of project?

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 1:09pm UTC](https://forum.freecodecamp.org/t/python-code-review/427129/8 "2021-06-01T13:09:42Z")

</div>

Closed on June 1, 2021, 1:09pm UTC


