[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
6/25/2020 5:12:35 AM𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥
55 AnswersNew Answer
👍 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.
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 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.
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 if int(hours) >= a: print("yes") also if int(hours) == a: print("yes")
What do you see with int("")?
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 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
Try starting a blank code and run with only int("")
What does it say?
Why are you using 'int' in the first condition and 'len' in the second?
What do you want to achieve? Can you give some input - output pair examples
What does it mean?
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 Is your question solved already?
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 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
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 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)
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 In your second version, after input1 = input() add print(input1) what do you see?
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 You are right
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 the second code is not addressing len but base '10' error
Use try and except in these cases Instead of using if elif and else
import re pattern = r"spam" if re.match(pattern, "spamspamspam"): print("Match") else: print("No match")
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
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