+ 1

Trying to understand programming

How did the computer get this answer!? >>> x = 2 >>> print(x) 2 >>> x += 3 >>> print(x) 5

7th Apr 2018, 9:27 PM
Kareem Callwood
Kareem Callwood - avatar
6 Antworten
+ 3
x += 3 is shorthand for x = x + 3 Note that mathematically this wouldn't make sense, but in this case the equals sign is an assignment operator, and not an equality sign which is == in most programming languages.
7th Apr 2018, 9:42 PM
Emma
+ 1
which bit dont you understand?
7th Apr 2018, 9:30 PM
Obbu
Obbu - avatar
0
I don’t understand how the computer got 5?
7th Apr 2018, 11:25 PM
Kareem Callwood
Kareem Callwood - avatar
0
@Xan will x= 2
7th Apr 2018, 11:29 PM
Kareem Callwood
Kareem Callwood - avatar
0
Kareem Callwood X= 2 X+=3 ( x = X(2) + 3 ) X = 5
8th Apr 2018, 4:37 AM
Kuldeep Singh
Kuldeep Singh - avatar
0
ok x ends up = 5 because firstly you have set x = 2 so now x has the value of 2 as shown by printing it when you do x + = 3 short for x = x + 3 which when you substitute the value of x in is x = 2 + 3 which gives you the answer 5
8th Apr 2018, 9:12 AM
Obbu
Obbu - avatar