Problem with input and return in python app for count words
up vote
0
down vote
favorite
i'm just starting to learn now and i have been doing some exercises trying to add some inputs to the basics functions i have made .
right now i have this code.
print("This is an app calculate the lenght of a word")
def String_Lenght(word):
if type(word) == int:
return "Integers can't be counted"
elif type(word) == float:
return "floats can't be counted"
else:
return len(word)
word = input("enter the word")
print(String_Lenght(word))
The problem is that i get the len of the word but i'm not getting the messages for int and floats in the case when i introduce one, which would be the error here.
Thanks/
python
add a comment |
up vote
0
down vote
favorite
i'm just starting to learn now and i have been doing some exercises trying to add some inputs to the basics functions i have made .
right now i have this code.
print("This is an app calculate the lenght of a word")
def String_Lenght(word):
if type(word) == int:
return "Integers can't be counted"
elif type(word) == float:
return "floats can't be counted"
else:
return len(word)
word = input("enter the word")
print(String_Lenght(word))
The problem is that i get the len of the word but i'm not getting the messages for int and floats in the case when i introduce one, which would be the error here.
Thanks/
python
5
input()
always returns a string. If you input5
it is just a string of value'5'
.
– sashaaero
Nov 13 at 1:58
2
Possible duplicate of How can I read inputs as integers?
– James
Nov 13 at 2:00
You can use a try/except statement in order to see whether the input can get converted into an int / a float.
– quant
Nov 13 at 2:02
@quant You can check my answer below . I think I do porvide a more elegant solution .
– Mark White
Nov 13 at 3:17
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
i'm just starting to learn now and i have been doing some exercises trying to add some inputs to the basics functions i have made .
right now i have this code.
print("This is an app calculate the lenght of a word")
def String_Lenght(word):
if type(word) == int:
return "Integers can't be counted"
elif type(word) == float:
return "floats can't be counted"
else:
return len(word)
word = input("enter the word")
print(String_Lenght(word))
The problem is that i get the len of the word but i'm not getting the messages for int and floats in the case when i introduce one, which would be the error here.
Thanks/
python
i'm just starting to learn now and i have been doing some exercises trying to add some inputs to the basics functions i have made .
right now i have this code.
print("This is an app calculate the lenght of a word")
def String_Lenght(word):
if type(word) == int:
return "Integers can't be counted"
elif type(word) == float:
return "floats can't be counted"
else:
return len(word)
word = input("enter the word")
print(String_Lenght(word))
The problem is that i get the len of the word but i'm not getting the messages for int and floats in the case when i introduce one, which would be the error here.
Thanks/
python
python
asked Nov 13 at 1:53
Carlos Leotaud
1
1
5
input()
always returns a string. If you input5
it is just a string of value'5'
.
– sashaaero
Nov 13 at 1:58
2
Possible duplicate of How can I read inputs as integers?
– James
Nov 13 at 2:00
You can use a try/except statement in order to see whether the input can get converted into an int / a float.
– quant
Nov 13 at 2:02
@quant You can check my answer below . I think I do porvide a more elegant solution .
– Mark White
Nov 13 at 3:17
add a comment |
5
input()
always returns a string. If you input5
it is just a string of value'5'
.
– sashaaero
Nov 13 at 1:58
2
Possible duplicate of How can I read inputs as integers?
– James
Nov 13 at 2:00
You can use a try/except statement in order to see whether the input can get converted into an int / a float.
– quant
Nov 13 at 2:02
@quant You can check my answer below . I think I do porvide a more elegant solution .
– Mark White
Nov 13 at 3:17
5
5
input()
always returns a string. If you input 5
it is just a string of value '5'
.– sashaaero
Nov 13 at 1:58
input()
always returns a string. If you input 5
it is just a string of value '5'
.– sashaaero
Nov 13 at 1:58
2
2
Possible duplicate of How can I read inputs as integers?
– James
Nov 13 at 2:00
Possible duplicate of How can I read inputs as integers?
– James
Nov 13 at 2:00
You can use a try/except statement in order to see whether the input can get converted into an int / a float.
– quant
Nov 13 at 2:02
You can use a try/except statement in order to see whether the input can get converted into an int / a float.
– quant
Nov 13 at 2:02
@quant You can check my answer below . I think I do porvide a more elegant solution .
– Mark White
Nov 13 at 3:17
@quant You can check my answer below . I think I do porvide a more elegant solution .
– Mark White
Nov 13 at 3:17
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
You can use ast.literal_eval() function to evaluate the string to either the integer or the floating point number and then use your code to count the length of the string.
from ast import literal_eval
print("This is an app calculate the length of a word")
def String_Lenght(word):
try:
word = literal_eval(word)
except ValueError:
pass
if type(word) == int:
return "Integers can't be counted"
elif type(word) == float:
return "floats can't be counted"
else:
return len(word)
word = input("enter the word")
print(String_Lenght(word))
add a comment |
up vote
0
down vote
When you read 'word' is always a string in python 3+.
so type(word)
is always string. hence you would get the length. Check the output below for your program. i used hard breakpoint using import pdb; pdb.set_trace()
instead of trying to check type(word). I think you should convert string to int / float.
I think converting to float is a best option .
This issue is in python 3. as all data from input is string. you can check here.
$ python t.py
This is an app calculate the lenght of a word
enter the word1
> /Users/sesh/tmp/t.py(6)String_Lenght()
-> if type(word) == int:
(Pdb) word
'1'
(Pdb) type (word)
<class 'str'>
(Pdb) int(word)
1
(Pdb) float(word)
1.0
(Pdb) int('asdfads)
*** SyntaxError: EOL while scanning string literal
(Pdb)
Traceback (most recent call last):
File "t.py", line 13, in <module>
print(String_Lenght(word))
File "t.py", line 6, in String_Lenght
if type(word) == int:
File "t.py", line 6, in String_Lenght
if type(word) == int:
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/bdb.py", line 51, in trace_dispatch
return self.dispatch_line(frame)
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/bdb.py", line 70, in dispatch_line
if self.quitting: raise BdbQuit
bdb.BdbQuit
(qsic-api-django)
sesh at Seshs-MacBook-Pro in ~/tmp
add a comment |
up vote
0
down vote
The reason of this problem is in Python input function always return str type.
So in your code type(word) always return True.
You should change your code to this.
print("This is an app calculate the lenght of a word")
def String_Lenght(word):
if word.isdigit():
return "Integers can't be counted"
elif word.replace(".", "", 1).isdigit():
return "floats can't be counted"
else:
return len(word)
word = input("enter the word")
print(String_Lenght(word))
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You can use ast.literal_eval() function to evaluate the string to either the integer or the floating point number and then use your code to count the length of the string.
from ast import literal_eval
print("This is an app calculate the length of a word")
def String_Lenght(word):
try:
word = literal_eval(word)
except ValueError:
pass
if type(word) == int:
return "Integers can't be counted"
elif type(word) == float:
return "floats can't be counted"
else:
return len(word)
word = input("enter the word")
print(String_Lenght(word))
add a comment |
up vote
1
down vote
You can use ast.literal_eval() function to evaluate the string to either the integer or the floating point number and then use your code to count the length of the string.
from ast import literal_eval
print("This is an app calculate the length of a word")
def String_Lenght(word):
try:
word = literal_eval(word)
except ValueError:
pass
if type(word) == int:
return "Integers can't be counted"
elif type(word) == float:
return "floats can't be counted"
else:
return len(word)
word = input("enter the word")
print(String_Lenght(word))
add a comment |
up vote
1
down vote
up vote
1
down vote
You can use ast.literal_eval() function to evaluate the string to either the integer or the floating point number and then use your code to count the length of the string.
from ast import literal_eval
print("This is an app calculate the length of a word")
def String_Lenght(word):
try:
word = literal_eval(word)
except ValueError:
pass
if type(word) == int:
return "Integers can't be counted"
elif type(word) == float:
return "floats can't be counted"
else:
return len(word)
word = input("enter the word")
print(String_Lenght(word))
You can use ast.literal_eval() function to evaluate the string to either the integer or the floating point number and then use your code to count the length of the string.
from ast import literal_eval
print("This is an app calculate the length of a word")
def String_Lenght(word):
try:
word = literal_eval(word)
except ValueError:
pass
if type(word) == int:
return "Integers can't be counted"
elif type(word) == float:
return "floats can't be counted"
else:
return len(word)
word = input("enter the word")
print(String_Lenght(word))
answered Nov 13 at 3:15
Rishabh Mishra
594
594
add a comment |
add a comment |
up vote
0
down vote
When you read 'word' is always a string in python 3+.
so type(word)
is always string. hence you would get the length. Check the output below for your program. i used hard breakpoint using import pdb; pdb.set_trace()
instead of trying to check type(word). I think you should convert string to int / float.
I think converting to float is a best option .
This issue is in python 3. as all data from input is string. you can check here.
$ python t.py
This is an app calculate the lenght of a word
enter the word1
> /Users/sesh/tmp/t.py(6)String_Lenght()
-> if type(word) == int:
(Pdb) word
'1'
(Pdb) type (word)
<class 'str'>
(Pdb) int(word)
1
(Pdb) float(word)
1.0
(Pdb) int('asdfads)
*** SyntaxError: EOL while scanning string literal
(Pdb)
Traceback (most recent call last):
File "t.py", line 13, in <module>
print(String_Lenght(word))
File "t.py", line 6, in String_Lenght
if type(word) == int:
File "t.py", line 6, in String_Lenght
if type(word) == int:
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/bdb.py", line 51, in trace_dispatch
return self.dispatch_line(frame)
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/bdb.py", line 70, in dispatch_line
if self.quitting: raise BdbQuit
bdb.BdbQuit
(qsic-api-django)
sesh at Seshs-MacBook-Pro in ~/tmp
add a comment |
up vote
0
down vote
When you read 'word' is always a string in python 3+.
so type(word)
is always string. hence you would get the length. Check the output below for your program. i used hard breakpoint using import pdb; pdb.set_trace()
instead of trying to check type(word). I think you should convert string to int / float.
I think converting to float is a best option .
This issue is in python 3. as all data from input is string. you can check here.
$ python t.py
This is an app calculate the lenght of a word
enter the word1
> /Users/sesh/tmp/t.py(6)String_Lenght()
-> if type(word) == int:
(Pdb) word
'1'
(Pdb) type (word)
<class 'str'>
(Pdb) int(word)
1
(Pdb) float(word)
1.0
(Pdb) int('asdfads)
*** SyntaxError: EOL while scanning string literal
(Pdb)
Traceback (most recent call last):
File "t.py", line 13, in <module>
print(String_Lenght(word))
File "t.py", line 6, in String_Lenght
if type(word) == int:
File "t.py", line 6, in String_Lenght
if type(word) == int:
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/bdb.py", line 51, in trace_dispatch
return self.dispatch_line(frame)
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/bdb.py", line 70, in dispatch_line
if self.quitting: raise BdbQuit
bdb.BdbQuit
(qsic-api-django)
sesh at Seshs-MacBook-Pro in ~/tmp
add a comment |
up vote
0
down vote
up vote
0
down vote
When you read 'word' is always a string in python 3+.
so type(word)
is always string. hence you would get the length. Check the output below for your program. i used hard breakpoint using import pdb; pdb.set_trace()
instead of trying to check type(word). I think you should convert string to int / float.
I think converting to float is a best option .
This issue is in python 3. as all data from input is string. you can check here.
$ python t.py
This is an app calculate the lenght of a word
enter the word1
> /Users/sesh/tmp/t.py(6)String_Lenght()
-> if type(word) == int:
(Pdb) word
'1'
(Pdb) type (word)
<class 'str'>
(Pdb) int(word)
1
(Pdb) float(word)
1.0
(Pdb) int('asdfads)
*** SyntaxError: EOL while scanning string literal
(Pdb)
Traceback (most recent call last):
File "t.py", line 13, in <module>
print(String_Lenght(word))
File "t.py", line 6, in String_Lenght
if type(word) == int:
File "t.py", line 6, in String_Lenght
if type(word) == int:
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/bdb.py", line 51, in trace_dispatch
return self.dispatch_line(frame)
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/bdb.py", line 70, in dispatch_line
if self.quitting: raise BdbQuit
bdb.BdbQuit
(qsic-api-django)
sesh at Seshs-MacBook-Pro in ~/tmp
When you read 'word' is always a string in python 3+.
so type(word)
is always string. hence you would get the length. Check the output below for your program. i used hard breakpoint using import pdb; pdb.set_trace()
instead of trying to check type(word). I think you should convert string to int / float.
I think converting to float is a best option .
This issue is in python 3. as all data from input is string. you can check here.
$ python t.py
This is an app calculate the lenght of a word
enter the word1
> /Users/sesh/tmp/t.py(6)String_Lenght()
-> if type(word) == int:
(Pdb) word
'1'
(Pdb) type (word)
<class 'str'>
(Pdb) int(word)
1
(Pdb) float(word)
1.0
(Pdb) int('asdfads)
*** SyntaxError: EOL while scanning string literal
(Pdb)
Traceback (most recent call last):
File "t.py", line 13, in <module>
print(String_Lenght(word))
File "t.py", line 6, in String_Lenght
if type(word) == int:
File "t.py", line 6, in String_Lenght
if type(word) == int:
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/bdb.py", line 51, in trace_dispatch
return self.dispatch_line(frame)
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/bdb.py", line 70, in dispatch_line
if self.quitting: raise BdbQuit
bdb.BdbQuit
(qsic-api-django)
sesh at Seshs-MacBook-Pro in ~/tmp
answered Nov 13 at 2:09
Seshadri VS
18313
18313
add a comment |
add a comment |
up vote
0
down vote
The reason of this problem is in Python input function always return str type.
So in your code type(word) always return True.
You should change your code to this.
print("This is an app calculate the lenght of a word")
def String_Lenght(word):
if word.isdigit():
return "Integers can't be counted"
elif word.replace(".", "", 1).isdigit():
return "floats can't be counted"
else:
return len(word)
word = input("enter the word")
print(String_Lenght(word))
add a comment |
up vote
0
down vote
The reason of this problem is in Python input function always return str type.
So in your code type(word) always return True.
You should change your code to this.
print("This is an app calculate the lenght of a word")
def String_Lenght(word):
if word.isdigit():
return "Integers can't be counted"
elif word.replace(".", "", 1).isdigit():
return "floats can't be counted"
else:
return len(word)
word = input("enter the word")
print(String_Lenght(word))
add a comment |
up vote
0
down vote
up vote
0
down vote
The reason of this problem is in Python input function always return str type.
So in your code type(word) always return True.
You should change your code to this.
print("This is an app calculate the lenght of a word")
def String_Lenght(word):
if word.isdigit():
return "Integers can't be counted"
elif word.replace(".", "", 1).isdigit():
return "floats can't be counted"
else:
return len(word)
word = input("enter the word")
print(String_Lenght(word))
The reason of this problem is in Python input function always return str type.
So in your code type(word) always return True.
You should change your code to this.
print("This is an app calculate the lenght of a word")
def String_Lenght(word):
if word.isdigit():
return "Integers can't be counted"
elif word.replace(".", "", 1).isdigit():
return "floats can't be counted"
else:
return len(word)
word = input("enter the word")
print(String_Lenght(word))
answered Nov 13 at 3:14
Mark White
36729
36729
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272658%2fproblem-with-input-and-return-in-python-app-for-count-words%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
input()
always returns a string. If you input5
it is just a string of value'5'
.– sashaaero
Nov 13 at 1:58
2
Possible duplicate of How can I read inputs as integers?
– James
Nov 13 at 2:00
You can use a try/except statement in order to see whether the input can get converted into an int / a float.
– quant
Nov 13 at 2:02
@quant You can check my answer below . I think I do porvide a more elegant solution .
– Mark White
Nov 13 at 3:17