python - Python 3 错误(在我的打开列表中)仅返回三个项目
问题描述
您好,我在运行我的赛马程序时出错。基本上我只想在最后一部分输入名字,它应该为每个名字制作一匹马。一旦用户键入 XXX,命名停止,比赛开始。任何帮助都可以。我将向您展示程序是如何运行的。
我的错误仅在 E 部分
import random
print ("Random number from 10-21 is : " )
print (random.randrange(10,21))
##get the average
def average(times):
return sum(times) / len(times)
times = []
counter = 0
while counter < (1001):
counter += 1
times.append(random.randrange(10,21))
print (average(times))
## for one horse in the race
distance = 0
seconds = 1
while distance <= 10560:
distance += (random.randrange(4,41))
seconds += 1
print(distance)
##one horse running 1000 races
seconds = 0
distance = 0
times = []
counter = 0
while counter < (1001):
counter += 1
while distance <= 10560:
num = (random.randrange(4,41))
distance += num
seconds += 1
times.append(seconds)
print("Part C, Average of horses time", average(times))
##function that adds the random distance the horse
def horse(x):
b = random.randrange(4,41)
x = x + b
return x
##Part D
number_of_ = int(input("How many horses are in the race: "))
distance = []
for i in range(number_of_):
distance.append(0)
finishline = True
print(distance)
while finishline:
for p in range(len(distance)):
distance[p] = horse(distance[p])
if max(distance) <= 10560:
finishline = True
else:
finishline = False
print(distance)
print("The winner is" , max(distance))
#part E
name_of_horses = []
distance_of_horses = []
STOP = "XXX"
names = input("Enter a name for each horse: ")
while names != STOP:
name_of_horses.append(names)
names = input("Enter a name for each horse: ")
for i in range(len(names)):
distance_of_horses.append(0)
finishline = True
print(name_of_horses)
while finishline:
for p in range(len(distance_of_horses)):
distance_of_horses[p] = horse(distance_of_horses[p])
if max(distance_of_horses) <= 10560:
finishline = True
else:
finishline = False
print(distance_of_horses)
print("The winner is" , max(distance_of_horses))
下面提供的是程序当前返回的内容。我知道我是菜鸟,但任何帮助都可以。谢谢你。
==================
Random number from 10-21 is :
11
15.036963036963037
10567
Part C, Average of horses time 245.0
How many horses are in the race: 5
[0, 0, 0, 0, 0]
[10588, 10532, 10444, 10461, 10362]
The winner is 10588
Enter a name for each horse: dom
Enter a name for each horse: dom
Enter a name for each horse: dom
Enter a name for each horse: dom
Enter a name for each horse: dom
Enter a name for each horse: dom
Enter a name for each horse: XXX
['dom', 'dom', 'dom', 'dom', 'dom', 'dom']
[10477, 10574, 10251]
The winner is 10574
解决方案
问题出在第 78 行
for i in range(len(names)):
distance_of_horses.append(0)
由于您的程序读取的名称被视为字符串(并且字符串是一个字符数组),它会将您的字符串视为字符数组,在这种情况下,“XXX”是 3 个字符,所以您的 distance_of_horses 当然会有 3 个值它
经典的拼写错误
注意:我认为这不应该在答案部分,但我还不允许发表评论
推荐阅读
- python - 将 pyspark 数据框转换为 python 字典列表
- flutter - Flutter TabBarView 展开子
- mysql - 无法在 DDEV 项目中导出完整的 MariaDB
- azure - Azure API 管理 - 外部类型:网关无法访问虚拟网络中的资源?
- python - 使用 Oracle 游标插入 Sql Server 时,大量 Pyodbc 插入无法将 int 转换为 big
- azure-data-explorer - KQL 扩展到新列,里面有汇总
- xcode - 在 XCode 12.2 如何增加整个 UI 的字体大小
- c++ - 返回指针但不允许删除该指针的 C++ 类公共函数?
- node.js - 环境变量不适用于 Typescript 和 NodeJS
- javascript - jqwidgets图表不呈现任何数据