Reputation: 71
def count(string,letter):
count=0
for each in string:
if each==letter:
count=count+1
print count
is there a way to set the parameters to automatically accept the inputs as strings so the user doesn't have to add the quotations?
Upvotes: 2
Views: 133749
Reputation: 1
I've just proved it on Python 3 and it is working despite quotations are not used:
s=input('enter a string:')
l=input('enter a letter:')
def compuString(s,l):
count=0
for a in s:
if a==l:
count=count + 1
return count
print('count on repeated letter is:', compuString(s,l))
Upvotes: 0
Reputation: 700
Another option:
You can use input()
, like so:
def count(letter):
string=input() #<- user can type string into command line w/o quote
do_stuff
return bacon
But as far as passing a string as arguments without quotes, you can't do that. They'll be interpreted as variables.
Upvotes: 1
Reputation: 405
If you want a parameter as a string or float or etc, try this:
def add(x: float, y: float):
z = x + y
return z
Upvotes: 6
Reputation: 98
This is a complete version to use input()
def count(letter):
string = input() # <- user can type string into command line and end by 'Enter'
count = 0
for each in string:
if each == letter:
count += 1
print (count)
count('a') # run function 'count'
dsafjqaagreioa # <- input string
4 # <- result. Input string contains 4 'a'
Upvotes: 1
Reputation: 9853
is there a way to set the parameters to automatically accept the inputs as strings so the user doesn't have to add the quotations?
You must add the quotations so that the interpreter knows the variable is of type String so that the internal code of the method knows how to handle the parameter.
By default, there is no required type for your parameters for the count method so you could pass a dictionary, integer or list etc to this and it will still run the internal code of the method. However, your code is tailored towards a String variable and a character, so the user must provide the quotations in order for your method to execute as expected otherwise it will most likely result in an Error being raised.
def count(string, letter):
ctr = 0
for _letter in string:
if _letter == letter:
ctr += 1
print ctr
Upvotes: 0
Reputation: 310287
I think you're asking if you can call the function like this:
count(this is my string, i) # 3
instead of like this:
count('this is my string', 'i') # 3
And the answer is that there is no way to do this. The arguments are parsed when the source code is parsed and the source code parser would have no way to disambiguate a string with commas in it from multiple input arguments.
Of course, this is just one example of parser ambiguity. If a feature like that were to be proposed, I suppose that if you were really strict in the format of the input strings (no commas, extra syntax to disambiguate strings that have variable names from strings that didn't, etc) you could probably come up with a syntax to make it work. However, remembering all of that would be much more difficult than just enclosing your strings in quotes as they're meant to be (and it would make the python parser much more cumbersome to work with). So all in all, I would think it would be a net-loss for the language for them to introduce a feature like that (though I'm just one person who isn't the BDFL so don't mistake my opinion for the opinion of someone who matters).
Upvotes: 6
Reputation: 6141
def count(string,letter):
if not isinstance(string,str):
string = str(string)
count=0
for each in string:
if each==letter:
count=count+1
print count
Upvotes: -2