我无法将这两个值相乘。无论如何它都无法转换为整数。
tbl_breed = ["Red Chittagong", "Sussex", "Dexter", "Abondance",
"Sahiwal", "Vorderwald", "Ayrshire", "Jersey",
"Randall", "Alderney", "Carora", "Gloucester"]
tbl_rating = [1, 2, 3, 2, 3, 1, 2, 1, 2, 1, 3, 2]
tbl_count = [6, 3, 8, 7, 6, 4, 3, 7, 3, 3, 4, 7]
tbl_volume = [7.5, 5.7, 11.4, 11.4,
22.0, 15.2, 21.0, 18.3,
19.0, 9.0, 23.1, 16.0]
tbl_dailyVolume = []
dailyvol = 0
x = 0
while x <12:
totallist = [tbl_breed[x], tbl_rating[x], tbl_count[x], tbl_volume[x]]
x = x + 1
print(totallist)
for total in totallist:
namebreed = total[0]
rating = total[1]
numcows = total[2]
volmilk = total[3]
volmik = int(float(volmilk))
dailyvol = (numcows * volmilk)
print(namebreed, ":", dailyvol)
错误是:
Traceback (most recent call last):
File "/Users/squatpapa/Desktop/june2023 mock comp sci/Q06.py", line 30, in <module>
volmik = int(float(volmilk))
ValueError: could not convert string to float: 'u'
我只是想将值相乘来找到总数,然后在每个列表中的前一个值旁边循环打印它们并输出。
在第一个循环结束时,
totallist
是 ['Gloucester', 2, 7, 16.0]
。您应该亲眼看到这一点,因为循环中有 print(totallist)
。
然后第二个循环迭代该列表中的每个项目。
total
在第一次迭代时变为 'Gloucester'
,因此 namebread
是该字符串的第一个元素(字母 'G'
),rating
是该字符串的第二个元素(字母 'l')
,等等)开。
volmilk
是该字符串的第四个元素,字母 'u'
。
你只需要在
totallist
中制作一个列表:
tbl_breed = ["Red Chittagong", "Sussex", "Dexter", "Abondance",
"Sahiwal", "Vorderwald", "Ayrshire", "Jersey",
"Randall", "Alderney", "Carora", "Gloucester"]
tbl_rating = [1, 2, 3, 2, 3, 1, 2, 1, 2, 1, 3, 2]
tbl_count = [6, 3, 8, 7, 6, 4, 3, 7, 3, 3, 4, 7]
tbl_volume = [7.5, 5.7, 11.4, 11.4,
22.0, 15.2, 21.0, 18.3,
19.0, 9.0, 23.1, 16.0]
tbl_dailyVolume = []
dailyvol = 0
totallist = [[breed, rating, count, volume] for (breed, rating, count, volume) in zip(tbl_breed, tbl_rating, tbl_count, tbl_volume)]
for total in totallist:
namebreed = total[0]
rating = total[1]
numcows = total[2]
volmilk = total[3]
volmik = int(float(volmilk))
dailyvol = (numcows * volmilk)
print(namebreed, ":", dailyvol)
输出:
Red Chittagong : 45.0
Sussex : 17.1
Dexter : 91.2
Abondance : 79.8
Sahiwal : 132.0
Vorderwald : 60.8
Ayrshire : 63.0
Jersey : 128.1
Randall : 57.0
Alderney : 27.0
Carora : 92.4
Gloucester : 112.0