Explain this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

Explain this code?

Can anyone tell me what is the output of my code and why output is not coming for this code? https://code.sololearn.com/c7j0KNEgy0HI/?ref=app

26th Jul 2020, 6:10 AM
Bharat
Bharat - avatar
39 Answers
+ 21
You are taking input in string format. So while comparison, compare it with string, not integer. Ex: if n[0]=='7'
26th Jul 2020, 6:38 AM
Nova
Nova - avatar
+ 7
Mansi 👑 I also don't think you need two inputs. Something like this should suffice: number = input() if len(number) == 10 and (number[0] == '7' or number[0] == '8' or number[0] == '9'): print("valid number") else: print("invalid") Unless you need user to keep entering input, then you use a while loop.
26th Jul 2020, 6:47 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 6
Thank you soo much all of you I got my answer AKSHAY 🐰Tiara 🐰 Tomiwa Joseph Nova Aayush Saini
26th Jul 2020, 8:07 AM
Bharat
Bharat - avatar
+ 4
Mr.CODER it's okay you got your mistake that's enough by the way I hope you will get a great success in python
28th Jul 2020, 5:36 AM
Bharat
Bharat - avatar
+ 3
AKSHAY It is showing no output for your input in my mobile I don't know about your mobile
26th Jul 2020, 6:35 AM
Bharat
Bharat - avatar
+ 3
Tomiwa Joseph I want to write a program which validate mobile number Conditions are 1. Length of mobile number exactly 10 2. Mobile number must be starts with 7, 8 or 9
26th Jul 2020, 6:38 AM
Bharat
Bharat - avatar
+ 3
Nova Thank you soo much Sir I got my answer
26th Jul 2020, 6:42 AM
Bharat
Bharat - avatar
+ 3
Use this str(x).isdigit()
26th Jul 2020, 7:53 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 3
The code uses Boolean to compare String and Int which is wrong 🙄
27th Jul 2020, 7:23 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 3
Your correct one: Faults: 1. You took input as int, but int has no len(). So after entering the for loop you have to convert int to str. You cannot do it before for loop because range does not take input as str. 2. You have to use break, otherwise the function will repeat continuously. n = int(input()) for i in range(n): m = str(n) if len(m)==10 and (m[0]=='7' or m[0]=='8' or m[0]=='9'): print("Yes") break else: print("No") break
27th Jul 2020, 12:24 PM
Md. Faheem Hossain
Md. Faheem Hossain - avatar
+ 3
Mr.CODER Actually I want to input a number every time in for loop and in for loop int(input()) means it will runs how many times it depends upon the input number and for every time it will ask you a 10 digit number. Do you know python or not
28th Jul 2020, 3:36 AM
Bharat
Bharat - avatar
+ 2
Mansi 👑 It's working fine. Please explain what you want to do 😃😃 AKSHAY output is still no
26th Jul 2020, 6:34 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 2
I think this is valid code for ur problem statement N = input("enter phn number") if len(N)==10: if N[0]=="7" or N[0]=="8" or N[0]=="9" : print("yes") else: print("no")
26th Jul 2020, 7:17 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
🐰Tiara 🐰 I already got this but your program will give output yes if we input 8F54698745 but is it really a valid mobile number
26th Jul 2020, 7:25 AM
Bharat
Bharat - avatar
+ 2
If i will use letters it should be Invalid
26th Jul 2020, 7:36 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
🐰Tiara 🐰 but according to your program it is valid
26th Jul 2020, 7:52 AM
Bharat
Bharat - avatar
+ 2
Mansi 👑 yeah I forgot that it should be string. Use n[0] in ('7','8','9') 🙂
26th Jul 2020, 8:02 AM
AKSHAY🇮🇳
AKSHAY🇮🇳 - avatar
+ 2
You are taking input in string format. So while comparison, compare it with string, not integer. Ex: if n[0]=='7'
27th Jul 2020, 9:27 AM
Ashish
Ashish - avatar
+ 2
You did not put the statement in its block. I mean no starting { and ending }.
27th Jul 2020, 2:34 PM
Idris Azeez
Idris Azeez - avatar
+ 2
Mansi 👑 sorry i didnt saw that input the second input which means how many time the input should run , In hurry just wrote one input ... Sorry for wasting your time ... By the way i am beginner and learning
28th Jul 2020, 4:02 AM
__k_maaz__ [ INACTIVE ]
__k_maaz__ [ INACTIVE ] - avatar