The format string method is intended for this kind of task:
return 'salad: {} hamburger: {} water: {}'.format(s, h, w)
Unrelated: Your counting code is fragile. It happens to work correctly
with this data set, because the letters shw only appear once each in
the entire set of words. However, if you added a word like 'milkshake'
this code would count an extra s and h every time it appeared.
A better way would be to split the string into words, then look at the first letter of each:
for word in order.split():
char = word[0]
if char == 's':
etc.
Even more robust would be to count the words themselves and return a
dict, but I’ll leave that to you when you get to dicts; it looks like
you’re just beginning with Python.
The format string method is intended for this kind of task:
return 'salad: {} hamburger: {} water: {}'.format(s, h, w)
Unrelated: Your counting code is fragile. It happens to work correctly
with this data set, because the letters shw only appear once each in
the entire set of words. However, if you added a word like 'milkshake'
this code would count an extra s and h every time it appeared.
A better way would be to split the string into words, then look at the first letter of each:
for word in order.split():
char = word[0]
if char == 's':
etc.
Even more robust would be to count the words themselves and return a
dict, but I’ll leave that to you when you get to dicts; it looks like
you’re just beginning with Python.
Basically from the requirement which you mentioned, I believe you can try like this in C# :
static string OrderInfo(string order)
{
int len = order.Length; int count;
StringBuilder builder = new StringBuilder();
for(int i=0;i<len;i++)
{
count = 0;
char toSearch = order[i];
foreach(char c in order)
{
if (c == toSearch)
count++;
}
builder.Append("Letter " + order[i] + ": " + count);
}
return builder.ToString();
}
Note: This will include the repetitve letters also.
For a single returned value: you can't
The problem is that int(answer) is a number and '%' is a string, so you cant combine these in a return value - unless you convert both to the same data type.
Try converting both values to a string:
return "{}%".format(int(answer))
You could go totally overboard and define your own integer type that displays itself with a percent symbol:
class pint(int):
def __str__(self):
return super().__str__() + '%'
__repr__ = __str__
You can now make the return value of your function be pint(answer) and reap the benefits of having your number actually be a number while always printing as a percentage.
I don't really recommend this approach for your trivial case, but it could have its uses.
Videos
>>> import re
>>> string1 = "498results should get"
>>> int(re.search(r'\d+', string1).group())
498
If there are multiple integers in the string:
>>> list(map(int, re.findall(r'\d+', string1)))
[498]
An answer taken from ChristopheD here: https://stackoverflow.com/a/2500023/1225603
r = "456results string789"
s = ''.join(x for x in r if x.isdigit())
print int(s)
456789
Hello Everyone,
I was wondering if it's possible to get the input of a string from the user and convert it into an integer later on. I'm asking this as the code I'm working on requires a string input from the user of what service they'd like. I later had to add up the totals of the services and wondered if once the choice was selected or inputted then if it would become a number that later adds up to the total cost if multiple services are inputted.
To return a value, you simply use return instead of print:
def showxy(event):
xm, ym = event.x, event.y
x3 = xm*ym
return x3
Simplified example:
def print_val(a):
print a
>>> print_val(5)
5
def return_val(a):
return a
>>> result = return_val(8)
>>> print result
8
By using "return" you can return it out of the function. In addition you can specifie the datatype.
For Example:
def myFunction(myNumber):
myNumber = myNumber + 1
return int(myNumber)
print myFunction(1)
Output:
2
You can also display the datatype which you got returned out of the function with type()
print type( myFunction(1) )
Output:
<type 'int'>
class X:
def __init__(self):
self.x = 3.1415
def __str__(self):
return str(int(round(self.x)))
x = X()
print x
This does print 3
Just cast the value you return into string. In the Python data model,
object.__str__(self)Called by str(object) and the built-in functions format() and print() to compute the “informal” or nicely printable string representation of an object. The return value must be a string object.
From Python Data Model
The __str__ function is a built in function that must return a informal string representation for the object. You are returning an Integer representation.
So just change your return to str(int(round(self.x)))