我正在学习古塔格博士的《使用Python进行计算和编程入门》一书,并且正在完成第三章的练习。我遇到了麻烦,具体而言是在第25页的第3.2节中。练习要求:定义一个字符串s,其中包含由逗号分隔的一系列十进制数字,例如
我已经尝试了很多次,但仍然遇到各种错误。以下是我最近的一次尝试。
我收到了"ValueError: invalid literal for float(): 1.23,2.4,3.123."的错误信息。
s = '1.23,2.4,3.123'
。编写一个程序,打印出s中所有数字的总和。
先前的例子是:
total = 0
for c in '123456789':
total += int(c)
print total.
我已经尝试了很多次,但仍然遇到各种错误。以下是我最近的一次尝试。
total = 0
s = '1.23,2.4,3.123'
print s
float(s)
for c in s:
total += c
print c
print total
print 'The total should be ', 1.23+2.4+3.123
我收到了"ValueError: invalid literal for float(): 1.23,2.4,3.123."的错误信息。
total += c
。c
是一个字符串,所以你试图将一个字符串加到一个整数total
上。 - richizy