Zero experience python user needs help

Hi I am learning python but it’s not going well can some one explain something please.

“Ask and thou shalt receive”!
What is the problem you are tackling?
What have you coded so-far?
(and assuming you are attempting a course, which course or book are you using?)

1 Like

Hi I’m on LinkedIn learning and I have done Hello World lol a bit of ints and floats string Boolean. But I thought I’d have a go at an exercise and no matter how I did it it didn’t work. so this is the challenge, Exercise 1: Calculate the multiplication and sum of two numbers

Given two integer numbers, return their product only if the product is equal to or lower than 1000. Otherwise, return their sum.

Given 1:

number1 = 20 number2 = 30

Expected Output:

The result is 600

Given 2:

number1 = 40 number2 = 30

Expected Output:

The result is 70

and this is what I did but no matter what I do it don’t work. I don’t get it,

def multiplication_or_sum(num1, num2):
# calculate product of two number
product = num1 * num2
# check if product is less then 1000
if product <= 1000:
return product
# product is greater than 1000 calculate sum
return num1 + num2

first condition

result = multiplication_or_sum(20, 30)
print(“The result is”, result)

Second condition

result = multiplication_or_sum(40, 30)
print(“The result is”, result)

no clue what it means lol I have only been learning for like an hour

then I tried

number1 = 20
number2 = 30
if number1 <=1000:
print (‘number1’ + ‘number2’)

def multiplication_or_sum (num1, num2):
# calculate product of two number
prouct = num1 * num2
# check if product is less than 1000
if product <=1000:
return product

product is greater than 1000 calculate sum

return num1 + num2

first condition:

result = multiplication_or_sum (20, 30)
print (‘the result is’, result)

second condition

result = multiplication_or_sum (40, 30)
print (‘The result is’, result)

and all I get are syntax errors

When working with Python, indentation is important.
When working with this system, copy-paste the code from your IDE and use the </> formatter (ribbon-bar during Reply) to preserve spacing.
Also, please copy-paste ALL of the “stack trace” - the lines of error message, location within the code, etc.

sorry for being thick but I don’t know what an IDE is, but here are error messages:
File “/home/runner/variables/”, line 117
{set_a} {set_b}
SyntaxError: invalid syntax

I hope that makes sense, it probably doesn’t, like the console shows old stuff I was trying not the sum one I’m trying to workout, the solution on the site I used made zero sense, I have put the link in below.

File “/home/runner/variables/”, line 127
return num1 + num2
SyntaxError: ‘return’ outside function

I think ill give up

You can format your replies so that they are easy to read by doing this:
(note the three back ticks “`”)

def mycode():

IDE = code editor

The error talking about sets is entirely unconnected with the multiply/add script.

The “return outside function” error sounds like a problem with indentation (as above).

Personal opinions follow:
I took a quick look at the course quoted (emphasis on the “quick”!) and was confused. Perhaps goes over-board on the idea of “quickly” and makes assumptions - or perhaps you’ve been a little bit too fast for your own good. I am biased towards Internet courses and those university-led in-particular. Have not been impressed by LinkedIn, ie Lynda post-takeover; as a gross generalisation.

Recommend taking a look at Coursera and edX (I use the latter). Most of those will start-off with the basics (again, as-above) and whilst it might seem slow, is more-likely to lead to success. After all, if you can’t get out of the starting-blocks, how could you ever expect to win the race?

If that approach is too formal for your taste, have you noted The Python Tutorial - part of the Python documentation set?

1 Like

Brilliant I’ll check that out. I think u are right. I’ll slow down and read more.

I’ll have a go thank u.

Please read the pinned thread in order to understand how to make the code show up properly on the forum. In order for us to help you with the code, we need to be able to see it the same way it looks in your editor.

Just to make sure: you should use either an ordinary text editor like Notepad (or TextEdit, xed, gvim, whatever else you’re used to depending on your operating system…), or else an IDE (a program specially designed to organize the process of typing out and running the code, that can also give you some suggestions as you type, etc.). Do not use a word processor like Word or Wordpad (or LibreOffice etc.).

Also: please don’t tell people “it doesn’t work” - in order to understand a problem properly, we need to make sure we understand a) what you tried (not just the part where you typed the code, but also how you tried to make it run); b) what happened when you did that (what you can see happening), and c) how that is different from what you expected. If you get an error message, you should copy and paste a complete error message, and format it like the code.