Search code examples
pythonoutputtraceback

Traceback Output with functions


As you can see by my code, I'm trying to create a program of a Carpet fitting service! But I'm not sure why it stops when it gets to the second function! Can you analyse my code and try to show me what is wrong? I fear that you will only understand my problem if you see the rest of the code. Keep in mind, I am a very new beginner.

Problem (in code - i cannot locate it):

https://pastebin.com/SWMYEAfE

Output:

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:\Users\mikec\OneDrive - Ilford County High School\Computing HW.py", line 144, in <module>
    loopquality()
  File "C:\Users\mikec\OneDrive - Ilford County High School\Computing HW.py", line 28, in loopquality
    for x in range(nor):
TypeError: 'function' object cannot be interpreted as an integer
>>> 

Solution

  • There are two major errors that stand out to me immediately as I ran through your code:

    • Variable scope issues.
    • Attribute name overwriting issues.

    You are calling range on nor. I can see that you meant to call it on the variable nor that is defined inside of the function nor and not the function itself.

    There are two immediate solutions, either you create a variable called nor_val (or anything different from nor), in the scope above (where you defined all the other variables); and update that variable inside of your nor function and use it in the range call. The second option is to make your nor function return the nor attribute at the end of execution, and instead of calling range(nor) you would call range(nor()).

    In the first case, you would add a line like nor_val = 0 where you are declaring your attributes, and then replace all instances of nor where you mean the variable (not the function) with nor_val.

    In the second case, you would add a line like return nor at the end of your nor function, and change the range(nor) to be range(nor()).

    However, in both cases, your variable naming is not very intuitive. You should try to not reuse names as they could cause this type of confusion. Your nor function does not describe what the function actually does. I might call it something like get_number_rooms or something along those lines, and I would call the nor variable maybe rooms.