Python Forum
Homework, works but has logic error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Homework, works but has logic error
#1
Hi, I am new to the forum and posting on behalf of my son, who has just started doing Python at school. He has done the code below to show someone getting a £20 bonus for working more than 8 hours and an extra 30% bonus for working more than 12 hours.

It works and gives the desired output, but he can't submit it via the online homework software as it is saying there is a logic error. He thinks it might be something to do with an infinite loop but can't work out why. He isn't trying to cheat but reckons it must be a simple error. He has put the code in replit but it doesn't show an error. I think he has missed the submission date for this question anyway (today), but it would be useful to know why it is wrong so he doesn't do it again. Thank you.

hours = int(input("How many hours did you work today? "))
pay = 15
pay = pay * hours
if hours > 8:
  pay = pay + 20
if hours > 12:
  pay = pay + 20 * 1.3
elif pay < 8:
  pay

print("You earned ",pay)
hourly = pay / hours
print("You earned ",hourly,"per hour")
Reply
#2
I think that the issue is with line 8 / 9 which is looking to see if pay < 8. I don't think that's correct as it should be hours < 8, but even then, from what I can see, you could simply remove that branch, because if hours is less than 8, then the pay is simply the flat rate, right?
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
Thanks you are right about pay <8 should be hours <8, but I think that was a last minute typo when he was trying everything to fix it. I'll try changing that when he gets home. Thanks again.
rob101 likes this post
Reply
#4
  • Take a hypothetical situation where hours equal 15:
  • pay is initialized to 15
  • if hours > 8: pay = pay + 20
    This statement is True, pay = 15 + 20 = 35
  • Next you test if hours > 12: pay = pay + 20 * 1.3
    This statement is True as hours = 15 pay = 35 + 20 * 1.3 = 61
    I don't think you meant to add an additional 20
    Also, note that the multiplication has precedence over addition, so
    20 * 1.3 gets added to 35. If you wanted to multiply the sum by 1.3, use pay = (pay + 20) * 1.3
  • syntax is incorrect on lines 6 - 9

to exclude the error on line 6 use elif instead of if
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Logic error - I need User_1 and User_2 to become true when both users sign in. BruhMoments 3 1,921 Jan-20-2021, 10:26 PM
Last Post: BruhMoments
  Issue with logic in homework fad3r 5 3,585 May-24-2018, 08:13 AM
Last Post: Larz60+
  I have a logic error. forumer444 3 3,210 Sep-06-2017, 10:27 PM
Last Post: forumer444

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020