带有函数的回溯输出



您可以通过我的代码看到,我正在尝试创建一个地毯安装服务的程序!但是我不确定为什么它到达第二个功能时会停止!您可以分析我的代码并尝试向我展示出什么问题吗?我担心,只有看到其余的代码,您只会理解我的问题。请记住,我是一个非常新的初学者。

问题(在代码中 - 我找不到):

https://pastebin.com/SWMYEAfE

输出:

This is Carpet Diem Carpets and we hope we are able to help you today!
We first need your personal details, so can you please choose your title.
Mr, Mrs, Miss, Ms, Dr, Lady or Lord?Lord
Well, what is your last name, Lord?
>>Test
Here at Carpet Diem Carpets...
We offer a variety of different levels of quality - 
With varying prices, of course, Lord Test!
We can fit your carpets at £6.50 per square meter - the Basic level.
Standard for £18.75.
And, for those who like to live as the very best, Luxury for £29.50!
There is also a 3.75 fee per square meter.
How many rooms do you want 'carpeted'?
>>3
Traceback (most recent call last):
  File "C:UsersmikecOneDrive - Ilford County High SchoolComputing HW.py", line 144, in <module>
    loopquality()
  File "C:UsersmikecOneDrive - Ilford County High SchoolComputing HW.py", line 28, in loopquality
    for x in range(nor):
TypeError: 'function' object cannot be interpreted as an integer
>>> 

当我通过您的代码运行时,有两个主要的错误错误:

  • 可变范围问题。
  • 属性名称覆盖问题。

您在nor上调用range。我可以看到您打算在函数nor内定义的变量nor上调用它。

在上面的范围(在其中定义所有其他变量)中,有两个立即解决方案,您要么创建一个称为nor_val(或与nor不同)的变量;并在nor功能中更新该变量,然后在range调用中使用它。第二个选项是使您的nor功能在执行结束时返回nor属性,而不是调用range(nor),您会调用range(nor())

在第一种情况下,您将添加一个像nor_val = 0之类的行,在该行中声明属性,然后替换nor的所有实例,其中您是指用nor_val的变量(而不是功能)。

在第二种情况下,您将在nor函数末尾添加类似return nor的行,然后将range(nor)更改为range(nor())

但是,在这两种情况下,您的变量命名都不是很直观。您应该尝试不重复使用名称,因为它们可能会引起这种混乱。您的nor函数不会描述该函数的实际功能。我可能会称其为get_number_rooms之类的东西或类似的东西,我将nor变量也许是rooms

最新更新