New course! Every coder should learn Generative AI!
Try a free lesson+ 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.
+ 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
Have a look at this!
If the input is empty then it will show an error
https://code.sololearn.com/cpZ4gT214aHe/?ref=app
+ 1
I think you you have already understood the error by now.
Your second code can be fixed by adding "0" to input1 in the 1st if block. Like this:
if int(input1 + "0") >= 1001: