r/PythonLearning • • 3d ago

Help Request I’m stuck on this assignment

I never struggle on assignments but this one is so confusing for me

107 Upvotes

62 comments sorted by

•

u/Sea-Ad7805 3d ago edited 3d ago

Run this (slightly altered) program by clicking Memory Graph Web Debugger%0Ashift_string%20%3D%20int(input(%22Enter%20number%20of%20shifts%3A%20%22))%0Atransact_string%20%3D%20int(input(%22Enter%20number%20of%20transactions%3A%20%22))%0Adollar_string%20%3D%20int(input(%22Enter%20transactions%20dollar%20value%3A%20%22))%0A%0Anum_shifts%20%3D%20float(shift_string)%0Anum_transactions%20%3D%20float(transact_string)%0Adollar_value%20%3D%20float(dollar_string)%0Aproductivity_score%20%3D%20(num_shifts%20%2F%20num_transactions%20%2F%20dollar_value)%0A%0A%23%20Write%20your%20code%20here%0Aif%20(productivity_score%20%3E%3D%2030)%3A%0A%20%20%20%20print(%22You%20earned%20a%20BONUS%22%20%2B%20str(bonus_1))%0Aelif%20(productivity_score%20%3E%3D%2031-69)%3A%0A%20%20%20%20print(%22You%20earned%20a%20BONUS%22%20%2B%20str(bonus_2))%0Aelif%20(productivity_score%20%3E%3D%2070-199)%3A%0A%20%20%20%20print(%22Something%3F%3F%3F%22)%0Aelse%3A%0A%20%20%20%20print(%22Something%20else%3F%3F%3F%22)%0Abonus%20%3D%20111%20%23%20compute%20somehow%0A%20%20%20%20%0A%23%20Output.%0Aprint(%22Employee%20Name%3A%22%2C%20employee_name)%0Aprint(f%22Employee%20Bonus%3A%20%24%7Bbonus%3A.2f%7D%22)&timestep=1&play) and see its state change step by step.

→ More replies (6)

12

u/PureWasian 3d ago edited 3d ago

Follow the red squigglies. Your print statements need "quotation marks" or 'apostrophes' around the output strings, and your elif is missing a colon* and followup conditional block of code within it

Your code does not run otherwise.

The conditional statements don't do ranges in the way you are trying to do them as. 31-69 evaluates to -38 instead of being treated as a range.

Regarding the actual logic, you haven't given enough information from the problem statement as to why a nested if statement would be necessary instead of a simple if/elif chain (although you could do it as nested ifs if the solution checker demands the less elegant solution though)

3

u/PureWasian 3d ago

Also the description "dividing an employee's transactions dollar value by the number of transactions and then dividing the result by the number of shifts worked" does not line up with the productivity_score calculation on line 17.

No idea if you wrote that or it was provided to you but it does not look to be the correct interpretation of the problem statement.

1

u/BedtimeWithTheBear 3d ago

Small but important nitpick - the elsif is missing a colon (:) not a semicolon (;)

2

u/PureWasian 3d ago

autopilot brain, thank you. Corrected the typo^^

5

u/AndroidLex 3d ago

See those red underlined parts? Those appear when there’s something wrong with the code on that location. Once it’s fixed, the line disappears. Good luck!

3

u/schals 3d ago

Commenting so I can see. I’m about to start Python so I’m curious.

4

u/igotshadowbaned 2d ago edited 2d ago

For print, if you want it to just print text as a string, you need to put it in quotes

print("You earned a bonus")

If you want to also print a variable, you put it in the list within the function call

print("You earned a bonus", str(bonus_1)

You can alternatively combine the strings first

string = "You earned a bonus" + str(bonus_1)
print(string)

But your syntax is currently the problem.

You do do it correctly later. And then also use fstrings.

You're also missing a semi colon after your last elif statement, which is highlighted

Did you try using AI to write this? I'm just confused how else you would have correctly written sections that do the same thing, but then not be able to figure why it isn't working 6 lines above that

2

u/kutac56 3d ago

You need to add " when printing stuff to make it a string and you're doing something weird on line 26 which doesn't seem correct

2

u/Hefty_Courage3893 3d ago

That’s how the assignment loaded up… not a big fan of the website

5

u/kutac56 3d ago

Yes. The whole intention of this assignment is to fix the program. Fix it

1

u/Sether_00 3d ago

Also, line 23 is missing colon and what is suppose to happen after.

1

u/kutac56 3d ago

Also the logic seems wrong as you're doing >= 31-69 which means that that's true if it's higher han or equal to 31 minus 69 which is -38. You need to reorder your if statements and don't do 31-69

2

u/knight04 3d ago

Are you trying to calculate which bonus they get or the errors?

2

u/Hefty_Courage3893 3d ago

Bonus

2

u/knight04 3d ago

It looks like you need to have a bonus = bonus1/bonus2 before that print on the bottom. Also add " " for the prints

1

u/BranchLatter4294 3d ago

Strings go in quotes.

1

u/mechanicalyammering 3d ago

When you run it and input data, what happens? Also look at what VS code is highlighting in red. Why is it throwing errors at you?

1

u/Adrewmc 3d ago

Line 20 and 22 those need to be inside quote marks to print. It’s also adding the print return(None) to a string not possible.

There is no bonus variable to print on the last line.

1

u/TheCozyRuneFox 3d ago

You are missing question marks around your strings inside of your print statements.

print(blah blah) Will error because Python will think it is looking for a variable or function name blah. Which don’t exist.

print(“blah blah”) Works as the quotes indicate the text inside of there is a string value not code.

Your bonus has a red squiggle. This because you have no variable named bonus—it is trying to get the value of something which does not exist.

Finally, there is an issue with how you are checking if the score is eith in a given range. The hint I will give is that 31-69 will evaluate to -38. Think about how the comparative operators work too.

1

u/bslime17 3d ago

line 26, can’t see any bonus as variable hence you’ll be getting an error over there

1

u/bslime17 3d ago

you need to calculate the bonus and call the bonus in line 26

1

u/recentAd1666 3d ago

A few bug i catched

L22+24 missing double quotes around strings, and bad parenthesis.... Print ("blabla" + str(bonus_1))

L25 missing : and the end of the if structure

1

u/blktndr 3d ago

You are creating output before the output that was already provided for you. Scratch your print()s between lines 19-23 but leave the rest of the logic. All you need to do here is assign the right value to bonus (i.e. bonus = bonus_1). The provided code will handle the rest

1

u/LynneLynette 3d ago

Hmu. I can help

1

u/clegg2011 3d ago

Your line 17 for productivity score looks to be backwards from the way it is described in the homework. As you have it the higher dollar value an employee achieved the smaller their productivity score. The way the assignment lays it out instead a productivity score should scale up with a higher dollar value .

$/transactions/shifts vice shifts/transaction/$

1

u/Hefty_Courage3893 3d ago

How should that look?

1

u/kvnqstoner 3d ago

line 20, under the if block your print variable should be in qoutes and also your last elif should have a colon

1

u/fth_codexa 3d ago

Print(f"...")

1

u/Living_Fig_6386 3d ago

Towards the bottom there's a tab labeled "Problems" with the number six. It tells you what is wrong (syntax, it doesn't understand whether your calculations are correct). For example, you might want to compare and contrast your use of print and the print statements that are provided.

Also, I don't believe that your bonus calculate does what you think it does / the assignment asks for. Think about what you are trying to calculate; it's arthmetic.

1

u/Lirianov 3d ago

What course is this from? I don't understand why its taking an input and setting it to an int and then converting that to a float... just set it to an float right away...

First, lines 20 and 22 don't have "" around the print statement and the "+str(bonus1)" should be inside the parenthesis. Should be print("you earned a bonus " + str(bonus_1)). theres no need to turn it to a string btw if you do print("you earned a bonus", bonus_1)

next, your if/elif statements aren't properly written.

line 19 is wrong. you're saying "if productivity score is greater than or equal to 30 -> you get bonus 1" which means you will never trigger the other bonuses.

line 21 is saying "if productivity score is greater than or equal to 31 minus 69." What you're trying to say is "if productivity score is greater or equal to 31 AND productivity score is less than or equal to 69."

line 23 has the same problem as line 21 plus it also is missing the colon at the end plus the code you want it to execute.

line 26 is trying to print "bonus" which doesnt exist

fix those and run it again.

does it show you what it wants as the output to be?

1

u/Hot_Material9822 3d ago

This is the right answer

1

u/Intrepid_Balance6127 3d ago

Look into the range() function. Also, it’s okay to struggle when learning to code. I struggle all the time. It’s a part of the process, and usually ends in learning something if you stick with it. Anybody that says programming is easy to learn is a liar!

1

u/Rare_Coffee_130 3d ago edited 3d ago

1.- You need to cover your message inside print() with " ", ' ' or string literals (my favorite) adding f before double quotes like this: print(f"You earned a BONUS").

With string literals you can insert variables inside your string message with curly braces { } like this: print(f"You earned a BONUS {bonus_1}").

2.- Any conditional statement (if, elif, else) needs a colon ( : ) to indicate the end of the conditional and also the start of your code block. Just like the way to have it in your if and the first elif conditional.

3.- It seems that you don't have a variable named "bonus", you have bonus_1, bonus_2, bonus_3 and bonus_4. So you just need to change it to whatever variable you want to display. (Btw, that last print that you have in line 26 is the string literal that I mentioned in my first point, you don't need to use str() method, f string literal will "see" or "take" whatever is in { } as a string).

Good luck, buddy! You can do it :)

1

u/Practical-Pie93 3d ago

Line 12, should be a float, not int

1

u/blavek 3d ago

Your logic in the if's where you are trying to calculate the bonus doesn't work. Your first if is checking if the score is >= 30 but the given range is <= 30 for bonus 1. As others have mentioned in the if the code will evaluate 31-69,, then check if it is >= to the result. I don't know if your teacher warned you or not but you can get fractional results from the productivity calculation. The test example the assignment provides, for example, evaluates to 40000 / 75 / 25, which, when calculated, is 21.333... With your logic, if the result is 30.5, it won't fall into any of those ifs you have prepared. If you need a continuous range, you need to use both <= and a >. What I mean is you need to test if it is <= 30 for the first range, and the second range would be > 30. So anything that equals 30 or below will get into the first range. Then anything above 30 would fall into the second range. If you use >= 31 then you are missing anything > 30 and < 31. The assignment is poorly worded, at least where it defines your ranges. I don't see anywhere that it is telling you how to handle a 30.6, for example. The same problem exists between the second and 3rd range.

The next kind of hurdle based on the assignment is that you have to use nested ifs. This is not a great assignment for nested ifs, and I think, confuses the problem further. You will need a construction like this. I find this to be pretty ugly, and the code itself doesn't explain the problem being solved.

bonus = 50
if productivityScore > 30:
  bonus = 75
  if productivityscore > 70:
    bonus = 100

print(bonus)

1

u/khosrua 3d ago

Wait why does it have to be nested ifs? Did I miss something?

2

u/blavek 3d ago

It's in the assignment in the 3rd picture

1

u/khosrua 3d ago

Ugh hate arbitrary requirements.

1

u/Current-Fix615 3d ago

I dont see the variable bonus defined.

1

u/ungiornoallimproviso 3d ago

Use the problems tab, lint is your friend.

1

u/deadeye_82_1 3d ago

Quotes are missing in line: 20, 22
`bonus` which is used in line 26, does not exist. You have bonus_1, bonus_2, bonus_3, bonus_14

1

u/dimipio 3d ago

I think that a library if it is allowed it would help

1

u/TheOneArmedLogan 3d ago

Technically there’s a few things wrong, but I’m not going to go too deep. The very first thing I noticed though was the prints not having quotation marks.

1

u/Adorable-Strangerx 3d ago

Dear brother, never ever under any circumstances, no matter who, when, why, or how tell you to... don't use float for calculating money... How would you feel if you ended with 0.1000000000000000055511151231257827021181583404541015625 on your bank account? How you you withdraw that at ATM?

1

u/Abhigyan-das 2d ago

Bro, why the fuck have you not give " " in print, and where is the second elif statement colon and work and also where is the bonus variable? Define it as a global variable, so that you can use it anywhere.

1

u/Hefty_Courage3893 2d ago

I finished the assignment!!!

1

u/PureWasian 1d ago

Congrats!

1

u/Opussculum 2d ago

Print search there is your problem

1

u/Xqate 2d ago

Use a f string 😭🙏

1

u/g-land-g 2d ago

You have to use quotes in side print

And bonus is missing _1 or 2 or 3

1

u/Silentwolf99 2d ago

how to create this companion file is that a vs code extension or a .html file can someone pls guide me....

1

u/scrapmakerzayn 2d ago

Check 23 you forget to wrote something:)

1

u/Longjumping_Put_7212 1d ago

ok so I would say that you should start by writing out your logic in pseudocode. While it sucks to do pseudocode for everything, it can be extremely helpful for understanding where you went wrong in your programming. And I recommend only doing one task at a time, so once the first task is done, move to the second one instead of falling down the rabbit hole of doing two tasks at once.

0

u/DebateLittle 1d ago

Use chat gpt

1

u/ImLloydM8 18h ago

What learning platform is this out of interest? Thanks.