[Solved]Reason for not getting the Output | SoloLearn: Learn to code for FREE!

+10

[Solved]Reason for not getting the Output

I couldn't understand why my 2nd code is not working? But the 1st one is working fine input1=input() if len(input1)==0: print("Input1: Please enter a value") elif int(input1)>=1001: print("Input must be less than 1000") else: print("Input1 :",input1) ************************************ input1=input() if int(input1)>=1001: print("Input must be less than 1000") elif len(input1)==0: print("Input1: Please enter a value") else: print("Input1 :",input1) Please give me a reason with an another example Code is taken from this question https://www.sololearn.com/Discuss/2359545/?ref=app

55 Answers

New Answer

+6

👍 Just to put it explicit: Because int() can accept string such as "123" but it cannot accept string such as "". So in version 2, the error "base 10" appears, when your input1 is "" and the line int(input1) is reached. And in version 1, your if conditon is preventing int("") to happen, so there won't be the error message.

+8

𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 your 2nd attempt : You try to convert input to number before checking the length of input. So if input is empty, you will sure get an error. But in your 1st attempt, you check the length of input. If its length greater than 0 then convert it to number. So, your 1st is sure work. The problem in your link to another question: I think what he wanna ask is EOFERRER error. It means he will get that error definitely if he runs his code and submit input without splitting it into 2 lines (he needs to give 2 lines of input even if they are blank to avoid error) // this only sololearn playground.

+6

𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 if int(hours) >= a: print("yes") also if int(hours) == a: print("yes")

+6

What do you see with int("")?

+5

𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 error is not in this case! If you give no output or not a number, then the int part will show an error! print(len(str("")) == 0) Is not where your code is wrong

+5

Try starting a blank code and run with only int("")

+5

What does it say?

+4

Why are you using 'int' in the first condition and 'len' in the second?

+4

What do you want to achieve? Can you give some input - output pair examples

+4

What does it mean?

+3

𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 Is your question solved already?

+3

𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 let's look at this a bit different as int is a float not a natural number though it may appear to look as such thus you are trying to compare them as equals.... https://code.sololearn.com/cSIq2GujVmvf/?ref=app

+3

𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 your order of operations were skewed input1=input() if not input1: print("input1: Please enter a value") elif int(input1)>=1001: print("Input must be less than 1000") else: print("Input1 :",input1)

+3

𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 In your second version, after input1 = input() add print(input1) what do you see?

+3

𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 You are right

+2

𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 the second code is not addressing len but base '10' error

+2

Use try and except in these cases Instead of using if elif and else

+2

import re pattern = r"spam" if re.match(pattern, "spamspamspam"): print("Match") else: print("No match")

+1

You will have to convert it into a string then find its length! See this! This will help you https://code.sololearn.com/cu97E23K8Zmg/?ref=app

+1

Namit Jain Most important thing you should know that before answering ,you should check all the above comments Your code is not helping me anymore