Python:无法停止此程序';s哨兵环
所以我正在制作一个关于曲棍球运动员比赛的节目,我将记录他们的进球 下面是应该发生的事情:Python:无法停止此程序';s哨兵环,python,python-3.x,Python,Python 3.x,所以我正在制作一个关于曲棍球运动员比赛的节目,我将记录他们的进球 下面是应该发生的事情: Who scored? 4 Who scored? 5 Who scored? 6 Who scored? 4 Who scored? 3 Game still going?(y/n) y Who scored? 3 Who scored? 2 Who scored? 5 Who scored? 2 Who scored? 3 Game still going?(y/n) n Creating a his
Who scored? 4
Who scored? 5
Who scored? 6
Who scored? 4
Who scored? 3
Game still going?(y/n) y
Who scored? 3
Who scored? 2
Who scored? 5
Who scored? 2
Who scored? 3
Game still going?(y/n) n
Creating a histogram from values:
Element Goals Histogram
1 0
2 2 **
3 2 ***
4 2 **
5 2 **
6 1 *
7 1 *
8 0
9 0
10 0
这是我的密码:
def info():
ranking = [0,0,0,0,0,0,0,0,0,0,0]
survey = []
return ranking,survey
def info2(survey):
x = ''
for i in range(0,5):
x = int(input("Who scored?"))
survey.append(x)
again(x)
return survey
def info3(ranking,survey):
for i in range(len(survey)):
ranking[survey[i]]+=1
return ranking, survey
def again(x):
y = input("Game still on? y/n").lower()
if y == "yes" or y == "y":
info()
elif y == "n" or y =="no":
hg(x)
#create histogram
def hg():
print("\nCreating a histogram from values: ")
print("%3s %5s %7s"%("Element", "Goals", "Histogram"))
#start from element 1 instead of 0
for i in range(len(ranking)-1):
print("%7d %5d %-s"%(i+1, ranking[i+1], "*" * ranking[i+1]))
def main():
x,y = info()
a = info2(y)
d = again(x)
b,c = info3(x,a)
z = hg(x)
main()
当我按原样运行时,我得到了谁得分的东西,我在y/n上输入“y”,它就工作了,但当我输入y/n并输入n
,它会打印“元素目标直方图”,然后抛出以下内容:
Traceback (most recent call last):
line 48, in <module>
main()
line 44, in main
a = info2(y)
line 17, in info2
again(x)
line 29, in again
hg(x)
line 39, in hg
for i in range(len(ranking)-1):
NameError: name 'ranking' is not defined
回溯(最近一次呼叫最后一次):
第48行,在
main()
第44行,总机
a=info2(y)
第17行,在info2中
再次(x)
第29行,再次输入
hg(x)
第39行,单位为hg
对于范围内的i(len(排名)-1):
NameError:未定义名称“排名”
我发现代码中有几个问题
首先,在函数中使用了lower
而不是lower()
。这将函数本身绑定到x
,而不是调用它并分配其返回值
另外,您的hg()
函数需要一个参数,但您没有在这里传递一个参数。在info()
中定义的排名是该函数的本地项,在hg()
中不可见
在OP的代码根据我的上述评论更新后,编辑以响应OP:
此外,在再次()
中处理退出案例时也存在问题。我认为您根本不应该调用hg()
,而应该将答案返回到info2()
中的一个单独变量
因此,这两个函数的代码将如下所示:
def info2(survey):
x = ''
ans = 'y'
while ans in ('y', 'yes'):
for i in range(0,5):
x = int(input("Who scored?"))
survey.append(x)
ans = again()
return survey
def again():
x = input("Game still on? y/n").lower()
if x == "yes" or x == "y":
info()
return x
请注意附加变量的使用和传递过程
根据OP的第二条评论进行编辑:
info3()
保持不变。我添加了再次()
和info2()
,以及我的更改。您将保持info3()
不变(至少在这个特定问题上)
此外,由于我的更改在任何情况下都没有通过,因此实际上可以完全删除它。只需检查Yes情况,否则返回(在这个特殊情况下甚至不需要else
)
当我运行带有我提到的更改的代码时,它似乎按照要求工作。以下是示例输出:
Who scored?1
Who scored?1
Who scored?1
Who scored?2
Who scored?2
Game still on? y/ny
Who scored?3
Who scored?3
Who scored?3
Who scored?2
Who scored?2
Game still on? y/nn
Creating a histogram from values:
Element Goals Histogram
1 3 ***
2 4 ****
3 3 ***
4 0
5 0
6 0
7 0
8 0
9 0
10 0
我在代码中看到了几个问题
首先,在函数中使用了lower
而不是lower()
。这将函数本身绑定到x
,而不是调用它并分配其返回值
另外,您的hg()
函数需要一个参数,但您没有在这里传递一个参数。在info()
中定义的排名是该函数的本地项,在hg()
中不可见
在OP的代码根据我的上述评论更新后,编辑以响应OP:
此外,在再次()
中处理退出案例时也存在问题。我认为您根本不应该调用hg()
,而应该将答案返回到info2()
中的一个单独变量
因此,这两个函数的代码将如下所示:
def info2(survey):
x = ''
ans = 'y'
while ans in ('y', 'yes'):
for i in range(0,5):
x = int(input("Who scored?"))
survey.append(x)
ans = again()
return survey
def again():
x = input("Game still on? y/n").lower()
if x == "yes" or x == "y":
info()
return x
请注意附加变量的使用和传递过程
根据OP的第二条评论进行编辑:
info3()
保持不变。我添加了再次()
和info2()
,以及我的更改。您将保持info3()
不变(至少在这个特定问题上)
此外,由于我的更改在任何情况下都没有通过,因此实际上可以完全删除它。只需检查Yes情况,否则返回(在这个特殊情况下甚至不需要else
)
当我运行带有我提到的更改的代码时,它似乎按照要求工作。以下是示例输出:
Who scored?1
Who scored?1
Who scored?1
Who scored?2
Who scored?2
Game still on? y/ny
Who scored?3
Who scored?3
Who scored?3
Who scored?2
Who scored?2
Game still on? y/nn
Creating a histogram from values:
Element Goals Histogram
1 3 ***
2 4 ****
3 3 ***
4 0
5 0
6 0
7 0
8 0
9 0
10 0
我在代码中看到了几个问题
首先,在函数中使用了lower
而不是lower()
。这将函数本身绑定到x
,而不是调用它并分配其返回值
另外,您的hg()
函数需要一个参数,但您没有在这里传递一个参数。在info()
中定义的排名是该函数的本地项,在hg()
中不可见
在OP的代码根据我的上述评论更新后,编辑以响应OP:
此外,在再次()
中处理退出案例时也存在问题。我认为您根本不应该调用hg()
,而应该将答案返回到info2()
中的一个单独变量
因此,这两个函数的代码将如下所示:
def info2(survey):
x = ''
ans = 'y'
while ans in ('y', 'yes'):
for i in range(0,5):
x = int(input("Who scored?"))
survey.append(x)
ans = again()
return survey
def again():
x = input("Game still on? y/n").lower()
if x == "yes" or x == "y":
info()
return x
请注意附加变量的使用和传递过程
根据OP的第二条评论进行编辑:
info3()
保持不变。我添加了再次()
和info2()
,以及我的更改。您将保持info3()
不变(至少在这个特定问题上)
此外,由于我的更改在任何情况下都没有通过,因此实际上可以完全删除它。只需检查Yes情况,否则返回(在这个特殊情况下甚至不需要else
)
当我运行带有我提到的更改的代码时,它似乎按照要求工作。以下是示例输出:
Who scored?1
Who scored?1
Who scored?1
Who scored?2
Who scored?2
Game still on? y/ny
Who scored?3
Who scored?3
Who scored?3
Who scored?2
Who scored?2
Game still on? y/nn
Creating a histogram from values:
Element Goals Histogram
1 3 ***
2 4 ****
3 3 ***
4 0
5 0
6 0
7 0
8 0
9 0
10 0
我在代码中看到了几个问题
首先,在函数中使用了lower
而不是lower()
。这将函数本身绑定到x
,而不是调用它并分配其返回值
另外,您的hg()
函数需要一个参数,但您没有在这里传递一个参数。在info()
中定义的排名是该函数的本地项,在hg()
中不可见
编辑响应