+ 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
40 Respuestas
+ 21
You are taking input in string format. So while comparison, compare it with string, not integer.
Ex: if n[0]=='7'
+ 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.
+ 6
+ 3
AKSHAY It is showing no output for your input in my mobile I don't know about your mobile
+ 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
+ 3
Nova Thank you soo much Sir I got my answer
+ 3
Use this str(x).isdigit()
+ 3
The code uses Boolean to compare String and Int which is wrong 🙄
+ 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
+ 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")
+ 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
+ 2
If i will use letters it should be
Invalid
+ 2
🐰Tiara 🐰 but according to your program it is valid
+ 2
Mansi 👑 yeah I forgot that it should be string. Use n[0] in ('7','8','9')
🙂
+ 2
You are taking input in string format. So while comparison, compare it with string, not integer.
Ex: if n[0]=='7'
+ 2
You did not put the statement in its block. I mean no starting { and ending }.
+ 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