• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Can anyone help me with this?

Liam2233

Coder
HI guys, im not sure if this is the right place to ask but im doing this at the moment

Can anyone give me any advice?

Im awful at coding - for the first bit it said
Python:
expression2 = (5 + 6) * 2
print("expression2 is: " + str(expression2))

I assumed thats 5 plus 6 times by 2 which is obviously 11 but it says 17?

I
Then the best at the end that says 'try this' i tried it but it was a total mess and i needed a lie down, maybe coding isnt for me but just thought id see if any of you guys might be able to give a few poitners?

thanks
 
Last edited by a moderator:
As mentioned above, the result should and always will be 22, so, strangely, your output would be 17. My biggest thought is that you might be running the incorrect Python file with a formula that would result in 17. So just double-check that you are running the correct file.

Also, just as a little tip (to help you make your life far easier when printing in Python), rather than casting each of your data sets into str, we can actually make use of formatting.

Python:
expression2 = (5 + 6) * 2
print("expression2 is: " + str(expression2))
print(f"expression2 is: {expression2}")

Notice how in the second print statement, we have an added f before the quotation marks? This sets this string to be a formatted string, now whenever you want to print a variable inside your print, just place it inside {}, and this will automatically convert it to a string, removing the constant need to add the + and the str() before every variable.

Lastly, I do notice you are making a lot of comments about struggling with code and needing to rest. It's important to remember that everyone learns in different ways and at different paces. While some people can spend hours a day learning, others can only do it for 30 minutes at a time. You need to remember that the only way you will progress is if you put your mind to it and keep working hard, if you place yourself in the mindset of "this isn't for me" you'll eventually have no growth at all (which is far worse than struggling to learn). Just keep working hard and don't be so rough on yourself, find what works best for you and just keep practising.

Good luck with your coding journey. :))
 

Liam2233

Coder
Am i allowed to post links on here?

THis is the page im on?


apologies if im not
 

Liam2233

Coder
I dont think the link works, this is what i get

Just like in maths, certain arithmetic operations take precedence over others. What do you expect the result of the following expressions to be? Write down what you think each answer would be and then use the trinket IDE to create a program that contains this code. Run the program to find out if you correctly predicted the results for each expression.

Python:
expression2 = (5 + 6) * 2
print("expression2 is: " + str(expression2))
Copy


expression3 = 6 * 4 - 10 / 5 * 2
print("expression3 is: " + str(expression3))
Copy


expression4 = ((15 + 5) - 10) / 2
print("expression4 is: " + str(expression4))
Copy


I'm able to work the answers out in my head or on paper but when i type it into the 'trinket?' the answer seems off

And the next bit loses me completely it says
'
Using the trinket IDE below, write a program that calculates a Christmas budget:-
With Christmas coming up you want to save 25% of your salary in Sept, Oct, Nov after you've paid rent and utilities.

Assume you get paid 1250 per month, your rent is 650 and utilities are 175.

Print the amount you must save each month to meet this target?
Print the amount you will have in December for Christmas shopping'

I clicked on 'sample code' for some help and i got

Python:
   monthlySaving = (1250 - 650 - 175) * 25 / 100
print ("You must save " + str(monthlySaving) + " per month to meet your target"  )

salary = 1250 * 3
rent = 650 * 3
utilities = 175 * 3

decemberSaving = (salary - rent - utilities) * 25 / 100
print ("You will have " + str(decemberSaving) + " for Christmas shopping in December" )
Copy


None of that makes sense to me, and i get that its because im new to coding etc but how they do expect me to know this stuff, im basically stuck at this section aand cant progress until i can do this bit but i just dont see a way of getting past it
 
Last edited by a moderator:
Section 1:
For the math section trinket seems to output exactly what we're looking for mathematically, remember if we're following the rules of BODMAS (Brackets of division, multiplication, addition and subtraction) we know that anything in brackets should be calculated first, followed by division etc.

Let's take a look at each one of these.

Code:
(5 + 6) * 2

Because 5 + 6 is in brackets, this is what we'll calculate first, so that's 11; now we take that total and multiply it by 2, which results in 22

Code:
6 * 4 - 10 / 5 * 2

Because there are no brackets, we simply work from left to right.

Code:
6 * 4 = 24

24 - 10/5 x 2

10/5 = 2

24 - 2 x 2
24 - 4 (as 2x2 is 4)
= 20

Lastly, because there are two sets of brackets, we work with the inner ones outwards.

Code:
((15 + 5) - 10) / 2

15 + 5 = 20
20 - 10 = 10
10/2 = 5


For this section, let's break down the simple idea.

Python:
monthlySaving = (1250 - 650 - 175) * 25 / 100
print ("You must save " + str(monthlySaving) + " per month to meet your target" )

The above is all you actually need; because you've worked out the monthlySaving value, you can simply do the below

Python:
print(f"After three months you will have {monthlySaving * 3}")

#The reason we take the monthlySaving and multiply it by 3 is because we want to get the total after month one and just multiply it by three to get that answer after the three months.

Remember that this isn't exactly expecting you to write a whole block of code, but rather just to get a basic understanding of how to use maths in programming. I do know that if you get into their skills BootCamp they do explain everything in basic python code from the very beginning, so even if you don't fully understand it now, you will understand it if you get the acceptance seat.
 

Liam2233

Coder
So I get to the end bit and their sample says


Python:
monthlySaving = (1250 - 650 - 175) * 25 / 100
print ("You must save " + str(monthlySaving) + " per month to meet your target" )

MY job now is to tidy it up

so id put


Python:
monthlySaving = (1250 - 650 - 175) * 25 / 100
print ("1250 "  - 825  /25/100  )

I know that's wrong but its the best i can do
 
Last edited by a moderator:

Liam2233

Coder
Okay now I understand that i can just copy and paste the following into the trixet and it seems to work but i havent really learned much, am i to move onto the next section or should i try to learn it a bit more?

Python:
monthlySaving = (1250 - 650 - 175) * 25 / 100
print ("You must save " + str(monthlySaving) + " per month to meet your target"  )

salary = 1250 * 3
rent = 650 * 3
utilities = 175 * 3

decemberSaving = (salary - rent - utilities) * 25 / 100
print ("You will have " + str(decemberSaving) + " for Christmas shopping in December" )

How does the trinket know what 'you must save' means etc
 
Last edited by a moderator:
Okay now I understand that i can just copy and paste the following into the trixet and it seems to work but i havent really learned much, am i to move onto the next section or should i try to learn it a bit more?

monthlySaving = (1250 - 650 - 175) * 25 / 100
print ("You must save " + str(monthlySaving) + " per month to meet your target" )

salary = 1250 * 3
rent = 650 * 3
utilities = 175 * 3

decemberSaving = (salary - rent - utilities) * 25 / 100
print ("You will have " + str(decemberSaving) + " for Christmas shopping in December" )

How does the trinket know what 'you must save' means etc
I'd recommend doing more reading into it just to solidify your understanding.

The way it knows what "You must save" is because this is known as a string. Strings are used when we simply want to display text to the user. You can often identify a string by looking at the text and seeing if it has quotation marks around it. The rule of thumb is that if you see quotation marks you are most likely working with a string.

Because strings are just text Python will take whatever is inside the quotation marks and simply print that out into the console.
 

Liam2233

Coder

Why do we need Casting?​

Let’s create the following program to see why it is important to know how to use casting.

In the trinket IDE editor at the bottom of this page, type the following code:

number = input("Please enter a number")
answer = number * 10
print(number + " x 10 = " + answer)
Copy


On this one would i copy and paste exactly the bit where it says

number = input("Please enter a number")
answer = number * 10
print(number + " x 10 = " + answer)

Where it says 'please enter i number, does that mean I put an acutal number or leave the text in?
 
Top