- 1
[SOLVED]what is wrong with this code?
Whatever I input it says it is not a number. http://www.sololearn.com/app/JUMP_LINK__&&__python__&&__JUMP_LINK/playground/cJ916idlY3RJ/ Maybe it works on PC?
8 Answers
+ 4
I forgot my workaround to verify if user enter only digits:
if n.isdigit()==True and len(n)==1:
# ...
Also, you'll be adviced to use more space than only once to indent your code ^^
+ 3
@Boris:
It seems than randint() perform a dynamic cast, so no need to explicitly convert input to integer (even this could allow to test if digit by test if lesser than 10 and positive ;))
+ 2
Incidentaly, your link is wrong (but let us find your code anyway) ;)
Your main problem is the missuse of 'firstdigit': that's a method (function) and must have paenthesis to be called (and return a value), else you're comparing the method object itself (so never equals True ^^)
Your secondary problem is still in missused of firtdigit(): it test if a string is composed of only digits, but so, return True for numbers (many digits), and your code require integers lesser than 10 (to avoid error in the range passed at randint() arguments), all the more, that you confuse user by asking numbers and saying that's not numbers even if it is: you should use the word 'digit'...
Anyway, this code could work in playground (once time corrected), but with the well-known limitations in in/output: it will require giving the two expected input at once, separated by a new line, before custom messages would be displayed :P
+ 2
@Boris: I'm agree, but that's not the subject of the question ;)
+ 1
I suspect that you need to cast the input as integer.
number = int(input("Enter a number: "))
sum = number + 10
print(sum)
+ 1
@visp, Yes. I opened his code on the PC's browser to check it. And I think that it's unnecessary to put strings, if they are meant for numbers later. It complicates the code and the readability.
- 1
It only opens on Chrome for me. It is more than that.
- 1
I realised you could use 'try' and 'except' instead of checking if it is a digit if the user writes letters.
Here is the finished code (also improved)...
http://www.sololearn.com/app/JUMP_LINK__&&__python__&&__JUMP_LINK/playground/cJ916idlY3RJ/