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

7/26/2020 6:10:37 AM

Bharat

40 Answers

New Answer

+20

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

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[0]=='7' or m[0]=='8' or m[0]=='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[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