UnboundLocalError:在assignmen之前引用了局部变量“mp”

2024-06-16 10:53:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我在测试时得到这个错误:

Please enter the customer's name: 1
Please enter the customer's Phone Number:01506890639
Please enter the size of group: 4
Please enter the rating of the meal: 3
Do you wish to continue: Y/N?n


The largest group that visited had a cohort of:4
Traceback (most recent call last):
  File "E:/CWtsk/code2.1.py", line 93, in <module>
    main()
  File "E:/CWtsk/code2.1.py", line 12, in main
    mealrating(score)
  File "E:/CWtsk/code2.1.py", line 67, in mealrating
    mp = mp + 1
UnboundLocalError: local variable 'mp' referenced before assignment

我不确定引用的内容,因为正如您在我的代码中看到的,它以前没有被引用过。在

^{pr2}$

我不知道这是什么。虽然我需要调整最后一个功能,因为我忘记了它的故障排除。在


Tags: oftheinpymain错误linegroup
1条回答
网友
1楼 · 发布于 2024-06-16 10:53:06

您还没有定义mp,但已经在赋值的右侧使用了它。这将引发错误。通过以下方式轻松解决:

mp = 0 # <- Add this line and change the 0 if you expect something different
if score[x] >= 1 and score[x] <= 3:
        mp = mp + 1

相关问题 更多 >