 Explain this code? | Sololearn: Learn to code for FREE!

+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

+20

You are taking input in string format. So while comparison, compare it with string, not integer. Ex: if n=='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 == '7' or number == '8' or number == '9'): print("valid number") else: print("invalid") Unless you need user to keep entering input, then you use a while loop.

+6

Thank you soo much all of you I got my answer AKSHAY 🐰Tiara 🐰 Tomiwa Joseph Nova Aayush Saini

+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

+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=='7' or m=='8' or m=='9'): print("Yes") break else: print("No") break

+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

+2

Mansi 👑 It's working fine. Please explain what you want to do 😃😃 AKSHAY output is still no

+2

I think this is valid code for ur problem statement N = input("enter phn number") if len(N)==10: if N=="7" or N=="8" or N=="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 in ('7','8','9') 🙂

+2

You are taking input in string format. So while comparison, compare it with string, not integer. Ex: if n=='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