0

I am a beginner in python, Please tell me the error in this code.

I have tried a conditional statement, but the following code shows a syntax error in it. I have tried but every time it display the same message about error in the line 2. Please, elaborate. https://code.sololearn.com/ckJ4LWoYEQIA/?ref=app

11th Oct 2019, 1:15 PM
Robin Sain
Robin Sain - avatar
13 Answers
+ 2
No, else is required, or you can use another if
11th Oct 2019, 1:46 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
There should be ":" after "x<10". But I guess there will be another problem that doesn't cause an error: When x is smaller than 10, the program will print both "it's working" and "it's not working" To prevent this, you should change the code to this: if x<10: print('its working') else: print('its not working')
11th Oct 2019, 1:24 PM
ΛM!N
ΛM!N - avatar
+ 1
ΛM!N and you really think someone would exit the whole program just not to print something ?
11th Oct 2019, 1:52 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
Ayush Raj why are you repeating what ΛM!N said ?
12th Oct 2019, 12:30 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
0
if statement is always follower by : So: if x<10: #your code here
11th Oct 2019, 1:21 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
0
Yeah, I wanted to print only one of them...
11th Oct 2019, 1:44 PM
Robin Sain
Robin Sain - avatar
0
Can we print one of them without 'else'.......
11th Oct 2019, 1:45 PM
Robin Sain
Robin Sain - avatar
0
Thank you.....man.....problem solved.....
11th Oct 2019, 1:48 PM
Robin Sain
Robin Sain - avatar
0
Yw
11th Oct 2019, 1:49 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
0
Yes, there are some solutions if you don't want to use else. But using else is better. One of them is to exit and end the program in the first if statement: if x<10: print('its working') exit() print('its not working') Another way is to use another if or elif as Aymane Boukrouh said.
11th Oct 2019, 1:50 PM
ΛM!N
ΛM!N - avatar
0
Aymane Boukrouh You're right it can't be a good solution to use if the code is more complex
11th Oct 2019, 1:53 PM
ΛM!N
ΛM!N - avatar
- 1
x=7 if x<10: print('its working') else: print('its not working')
12th Oct 2019, 12:28 PM
Ayush Raj
Ayush Raj - avatar