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

+8

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

Mansi 👑

40 Answers

New Answer

+18

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

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")

+3

Use this str(x).isdigit()

+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

🐰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

The code uses Boolean to compare String and Int which is wrong 🙄

+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

+2

Mansi 👑 and i hope you too 😊