我正在学习python简介课程,所以这些内容仍然是相当基本的,但是如果有任何帮助,我们将不胜感激
我尝试过多种方法,我知道print语句中的逗号会自动添加空格,但我无法添加加号和句点而不出现错误
这是我的密码:
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)
给出输出:
After the 0.5 point bonus, my average is 87.6 .
当预期输出是句号正好在87.6后面的句子时
我尝试过以下方法:
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 + ".")
这给了我一个错误:
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'
命令以非零状态1退出
试试这个:
在Python3中,引入了F字符串以使这段代码更简单。见下文:
以下是了解更多信息的链接:https://realpython.com/python-f-strings/
使用f-strings:
相关问题 更多 >
编程相关推荐