Reputation: 37
I'm taking a python intro course so this stuff is still fairly basic, but any help would be greatly appreciated.
I've tried multiple things, I know that the comma in a print statement automatically adds a space, but I can't add a plus and the period without getting an error
here is my code:
bonus = survey_completers / class_size
avg = my_current_average + bonus
rounded_bonus = round(bonus, 1)
rounded_avg = round(avg, 1)
textOne = str("After the")
textTwo = str("point bonus, my average is")
textThree = str(".")
print(textOne, rounded_bonus, textTwo, rounded_avg, textThree)
giving the output:
After the 0.5 point bonus, my average is 87.6 .
When expected output is that sentence with the period right behind the 87.6
I've tried things such as:
bonus = survey_completers / class_size
avg = my_current_average + bonus
rounded_bonus = round(bonus, 1)
rounded_avg = round(avg, 1)
textOne = str("After the")
textTwo = str("point bonus, my average is")
print(textOne, rounded_bonus, textTwo, rounded_avg + ".")
which gives me this error:
Traceback (most recent call last):
File "CIOSBonus.py", line 40, in <module>
print(textOne, rounded_bonus, textTwo, rounded_avg + ".")
TypeError: unsupported operand type(s) for +: 'float' and 'str'
Command exited with non-zero status 1
Upvotes: 2
Views: 483
Reputation: 11581
the comma in a print statement automatically adds a space
Yeah, that's for convenience. When you type
print(x, y, z)
because you want to know what's in those variables, having an automatic space is easier than having to type this abomination:
print(x, " ", y, " ", z)
TypeError: unsupported operand type(s) for +: 'float' and 'str'
Yes, in python, you can't add a string and a float. You can convert the float to string:
print(textOne, rounded_bonus, textTwo, str(rounded_avg) + ".")
But you should really be doing:
print("After the %.1f point bonus, my average is %.1f." % (bonus, avg))
%.1f means a float rounded with 1 digit after the decimal point, so you can get rid of the line with round(). This is the standard printf() format, if you like archeology it comes from BCPL back in 1966, which explains why it looks weird. There is a more modern version in python3, see azro's answer.
textOne = str("After the")
Also you don't need to convert a string into a string. It's already a string.
Upvotes: 1
Reputation: 2915
You can convert the float to a string:
print(textOne, rounded_bonus, textTwo, str(rounded_avg) + ".")
You can also set sep=""
, default is sep=" "
print(textOne, " ", rounded_bonus, " ", textTwo, " ", rounded_avg, ".", sep="")
Another option is to use f-strings:
print(f"{textOne} {rounded_bonus} {textTwo} {rounded_avg}.")
You can also use string formatting:
print("%s %f %s %f." %(textOne, rounded_bonus, textTwo, rounded_avg))
%s is string
%i is int
%f is float
I think that using f-strings is the best and cleanest way.
Also, when creating a string you don't have to have str("")
.
For example:
x = "Test String"
Upvotes: 1
Reputation: 1794
The problem is you're trying to add a string to a float. That's not possible! Its like adding "a" + 2. Makes no sense, right? So, as the other answers have pointed out, you transform the float into a string:
str(rounded_avg)
That way python can add the "." to it. It's quite interesting that python can know how to "add" things together based on what those things are, you can read more about that here.
Also, its not necessary to wrap a string around str()
, as it is already a string. Remember, "strings are always surrounded by quotes".
So, knowing all that, your code would look like this:
bonus = survey_completers / class_size
avg = my_current_average + bonus
rounded_bonus = round(bonus, 1)
rounded_avg = round(avg, 1)
textOne = "After the"
textTwo = "point bonus, my average is"
print(textOne, str(rounded_bonus), textTwo, str(rounded_avg) + ".")
Even better, you could use formatted strings:
print(f"After the {rounded_bonus} point bonus, my average is {rounded_avg}.")
Upvotes: 1
Reputation: 18625
print
with multiple arguments always shows a space between the arguments, i.e. before textThree
in your example. If you replace the final print
function with one of these you won't get the spaces:
print(textOne + str(rounded_bonus) + textTwo + str(rounded_avg) + textThree)
or
print("After the {} points bonus, my average is {}.".format(rounded_bonus, rounded_avg))
or
print(f"After the {rounded_bonus} points bonus, my average is {rounded_avg}.")
Upvotes: 1
Reputation: 54168
You don't have to do str(" ")
, a surrounded quote value is already a string, but to join a number and a string, you first need to convert both of them to string
textOne = "After the"
textTwo = "point bonus, my average is"
print(textOne, rounded_bonus, textTwo, str(rounded_avg) + ".")
The better is still to use string formatting, here with f-string
, you can even handle the round
part in the format
bonus = 0.510101010
avg = 87.612121
print(f"After the {bonus:0.1f} point bonus, my average is {avg:0.1f}.")
Upvotes: 1
Reputation: 39
In python 3, F strings were introduced to make this bit of code a lot easier. See below:
rounded_bonus = 0.5
rounded_avg = 87.6
print(f'After the {rounded_bonus} point bonus, my average is {rounded_avg}.')
Here is a link to learn more: https://realpython.com/python-f-strings/
Upvotes: 1
Reputation: 22887
Using f-strings:
bonus = survey_completers / class_size
avg = my_current_average + bonus
rounded_bonus = round(bonus, 1)
rounded_avg = round(avg, 1)
result = f"After the {rounded_bonus} point bonus, my average is {rounded_avg}."
print(result)
Upvotes: 2
Reputation: 1106
Try this:
print('After the {} point bonus, my average is {}.'.format(rounded_bonus, rounded_avg))
Upvotes: 1