Problem with input and return in python app for count words
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 |
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 '18 at 1:58
2
Possible duplicate of How can I read inputs as integers?
– James
Nov 13 '18 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 '18 at 2:02
@quant You can check my answer below . I think I do porvide a more elegant solution .
– Mark White
Nov 13 '18 at 3:17
add a comment |
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 '18 at 1:53
Carlos LeotaudCarlos Leotaud
1
1
5
input()
always returns a string. If you input5
it is just a string of value'5'
.
– sashaaero
Nov 13 '18 at 1:58
2
Possible duplicate of How can I read inputs as integers?
– James
Nov 13 '18 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 '18 at 2:02
@quant You can check my answer below . I think I do porvide a more elegant solution .
– Mark White
Nov 13 '18 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 '18 at 1:58
2
Possible duplicate of How can I read inputs as integers?
– James
Nov 13 '18 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 '18 at 2:02
@quant You can check my answer below . I think I do porvide a more elegant solution .
– Mark White
Nov 13 '18 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 '18 at 1:58
input()
always returns a string. If you input 5
it is just a string of value '5'
.– sashaaero
Nov 13 '18 at 1:58
2
2
Possible duplicate of How can I read inputs as integers?
– James
Nov 13 '18 at 2:00
Possible duplicate of How can I read inputs as integers?
– James
Nov 13 '18 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 '18 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 '18 at 2:02
@quant You can check my answer below . I think I do porvide a more elegant solution .
– Mark White
Nov 13 '18 at 3:17
@quant You can check my answer below . I think I do porvide a more elegant solution .
– Mark White
Nov 13 '18 at 3:17
add a comment |
3 Answers
3
active
oldest
votes
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 |
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 |
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 |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 '18 at 3:15
Rishabh MishraRishabh Mishra
368310
368310
add a comment |
add a comment |
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 |
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 |
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 '18 at 2:09
Seshadri VSSeshadri VS
19314
19314
add a comment |
add a comment |
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 |
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 |
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 '18 at 3:14
Mark WhiteMark White
422310
422310
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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 '18 at 1:58
2
Possible duplicate of How can I read inputs as integers?
– James
Nov 13 '18 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 '18 at 2:02
@quant You can check my answer below . I think I do porvide a more elegant solution .
– Mark White
Nov 13 '18 at 3:17