Traceback error in Python for homework assignment - python

Getting a traceback error as follows:
Traceback (most recent call last):
File "F:\Python Codes\Falling Distance\hodge_Lab5b.py", line 12, in <module>
main()
File "F:\Python Codes\Falling Distance\hodge_Lab5b.py", line 9, in main
print(get_time, '\t', format(falling_distance, '.2f'))
TypeError: unsupported format string passed to function.__format__
#file 1 named hodge_Lab5b.py
def main():
from falling_distance import falling_distance
get_time = int(input("Enter the time, in seconds, the object has been falling: "))
print("Time",'\t' "Distance")
print("--------------------------")
for get_time in range (1,11):
print(get_time, '\t', format(falling_distance(main), '.2f'))
return get_time
main()
#File 2 named falling_distance.py
def falling_distance(main):
gravity = 9.8
fallingTime = main()
distance = (1/2)*gravity*(fallingTime**2)
return distance
Cannot figure out how to get these to work together. I do not know what I have done wrong. I have read through the relevant parts on the book several times. I feel like I am overlooking something rather simple and it is just not jumping out at me.

string formatting
old python: '%s %s' % ('one', 'two')
new python: '{} {}'.format('one', 'two')
also
for get_time in range(1,11): will iterate over 1,2,3...10
you probably want to do sth like this
for sec_nb in range(1, get_time+1):
print('falling time: {} \t falling dist: {}'.format(sec_nb, falling_distance(sec_nb)))
btw you want to pass number to falling_distance function, not a function

It seems like you have a few issues here. Since it's homework, I'm reluctant to provide any code. Here are a couple of issues that I see:
Naming. You're passing the function named main into the function called falling_distance. This is probably not what you want to do (definitely not with the provided code). If you carefully rename everything, I'm guessing most of the problems will go away with some extra debugging.
Inside calling_distance, you're again calling the function main which would seem to put you in an infinite loop. This is related to issue #1, but know that you'll need to revise how you're using the passed parameter in falling distance
If you're using python 2, I think you'll run into printing problems when printing out the table results
get_time is being reassigned in the for loop, which does not seem to be intended. You'll want to replace this with a new variable here
If you're using python 2, I expect that your falling_distance function will always return 0. This is because (1/2) is going to be interpreted as 0 (due to integer division). You should instead have the division happen last, once you know the numerator.
I see a few other issues as well, plus there are some style and convention issues that are more important long term.
Good luck

This is what I finally found to work. Thanks for your help.
#file 1
def main():
print( "Time\tFalling Distance\n-----------------------" )
from falling_distance import fallingDistance
for currentTime in range(1,11):
print(currentTime, "\t", format(fallingDistance(currentTime),\
".2f"))
main()
#file 2
def fallingDistance(fallingTime):
gravity=9.8
distance=(1/2)*gravity*(fallingTime**2)
return distance
Was certainly naming issues that caused a lot of the problems.
Thank you for your help!

Related

Function to return the line number that the function is called [duplicate]

I'm currently building quite a complex system in Python, and when I'm debugging I often put simple print statements in several scripts. To keep an overview I often also want to print out the file name and line number where the print statement is located. I can of course do that manually, or with something like this:
from inspect import currentframe, getframeinfo
print getframeinfo(currentframe()).filename + ':' + str(getframeinfo(currentframe()).lineno) + ' - ', 'what I actually want to print out here'
Which prints something like:
filenameX.py:273 - what I actually want to print out here
To make it more simple, I want to be able to do something like:
print debuginfo(), 'what I actually want to print out here'
So I put it into a function somewhere and tried doing:
from debugutil import debuginfo
print debuginfo(), 'what I actually want to print out here'
print debuginfo(), 'and something else here'
Unfortunately, I get:
debugutil.py:3 - what I actually want to print out here
debugutil.py:3 - and something else here
It prints out the file name and line number on which I defined the function, instead of the line on which I call debuginfo(). This is obvious, because the code is located in the debugutil.py file.
So my question is actually: How can I get the filename and line number from which this debuginfo() function is called?
The function inspect.stack() returns a list of frame records, starting with the caller and moving out, which you can use to get the information you want:
from inspect import getframeinfo, stack
def debuginfo(message):
caller = getframeinfo(stack()[1][0])
print("%s:%d - %s" % (caller.filename, caller.lineno, message)) # python3 syntax print
def grr(arg):
debuginfo(arg) # <-- stack()[1][0] for this line
grr("aargh") # <-- stack()[2][0] for this line
Output:
example.py:8 - aargh
If you put your trace code in another function, and call that from your main code, then you need to make sure you get the stack information from the grandparent, not the parent or the trace function itself
Below is a example of 3 level deep system to further clarify what I mean. My main function calls a trace function, which calls yet another function to do the work.
######################################
import sys, os, inspect, time
time_start = 0.0 # initial start time
def trace_libary_init():
global time_start
time_start = time.time() # when the program started
def trace_library_do(relative_frame, msg=""):
global time_start
time_now = time.time()
# relative_frame is 0 for current function (this one),
# 1 for direct parent, or 2 for grand parent..
total_stack = inspect.stack() # total complete stack
total_depth = len(total_stack) # length of total stack
frameinfo = total_stack[relative_frame][0] # info on rel frame
relative_depth = total_depth - relative_frame # length of stack there
# Information on function at the relative frame number
func_name = frameinfo.f_code.co_name
filename = os.path.basename(frameinfo.f_code.co_filename)
line_number = frameinfo.f_lineno # of the call
func_firstlineno = frameinfo.f_code.co_firstlineno
fileline = "%s:%d" % (filename, line_number)
time_diff = time_now - time_start
print("%13.6f %-20s %-24s %s" % (time_diff, fileline, func_name, msg))
################################
def trace_do(msg=""):
trace_library_do(1, "trace within interface function")
trace_library_do(2, msg)
# any common tracing stuff you might want to do...
################################
def main(argc, argv):
rc=0
trace_libary_init()
for i in range(3):
trace_do("this is at step %i" %i)
time.sleep((i+1) * 0.1) # in 1/10's of a second
return rc
rc=main(sys.argv.__len__(), sys.argv)
sys.exit(rc)
This will print something like:
$ python test.py
0.000005 test.py:39 trace_do trace within interface func
0.001231 test.py:49 main this is at step 0
0.101541 test.py:39 trace_do trace within interface func
0.101900 test.py:49 main this is at step 1
0.302469 test.py:39 trace_do trace within interface func
0.302828 test.py:49 main this is at step 2
The trace_library_do() function at the top is an example of something that you can drop into a library, and then call it from other tracing functions. The relative depth value controls which entry in the python stack gets printed.
I showed pulling out a few other interesting values in that function, like the line number of start of the function, the total stack depth, and the full path to the file. I didn't show it, but the global and local variables in the function are also available in inspect, as well as the full stack trace to all other functions below yours. There is more than enough information with what I am showing above to make hierarchical call/return timing traces. It's actually not that much further to creating the main parts of your own source level debugger from here -- and it's all mostly just sitting there waiting to be used.
I'm sure someone will object that I'm using internal fields with data returned by the inspect structures, as there may well be access functions that do this same thing for you. But I found them in by stepping through this type of code in a python debugger, and they work at least here. I'm running python 2.7.12, your results might very if you are running a different version.
In any case, I strongly recommend that you import the inspect code into some python code of your own, and look at what it can provide you -- Especially if you can single step through your code in a good python debugger. You will learn a lot on how python works, and get to see both the benefits of the language, and what is going on behind the curtain to make that possible.
Full source level tracing with timestamps is a great way to enhance your understanding of what your code is doing, especially in more of a dynamic real time environment. The great thing about this type of trace code is that once it's written, you don't need debugger support to see it.
An update to the accepted answer using string interpolation and displaying the caller's function name.
import inspect
def debuginfo(message):
caller = inspect.getframeinfo(inspect.stack()[1][0])
print(f"{caller.filename}:{caller.function}:{caller.lineno} - {message}")
The traceprint package can now do that for you:
import traceprint
def func():
print(f'Hello from func')
func()
# File "/traceprint/examples/example.py", line 6, in <module>
# File "/traceprint/examples/example.py", line 4, in func
# Hello from func
PyCharm will automatically make the file link clickable / followable.
Install via pip install traceprint.
Just put the code you posted into a function:
from inspect import currentframe, getframeinfo
def my_custom_debuginfo(message):
print getframeinfo(currentframe()).filename + ':' + str(getframeinfo(currentframe()).lineno) + ' - ', message
and then use it as you want:
# ... some code here ...
my_custom_debuginfo('what I actually want to print out here')
# ... more code ...
I recommend you put that function in a separate module, that way you can reuse it every time you need it.
Discovered this question for a somewhat related problem, but I wanted more details re: the execution (and I didn't want to install an entire call graph package).
If you want more detailed information, you can retrieve a full traceback with the standard library module traceback, and either stash the stack object (a list of tuples) with traceback.extract_stack() or print it out with traceback.print_stack(). This was more suitable for my needs, hope it helps someone else!

Debugging: Get filename and line number from which a function is called?

I'm currently building quite a complex system in Python, and when I'm debugging I often put simple print statements in several scripts. To keep an overview I often also want to print out the file name and line number where the print statement is located. I can of course do that manually, or with something like this:
from inspect import currentframe, getframeinfo
print getframeinfo(currentframe()).filename + ':' + str(getframeinfo(currentframe()).lineno) + ' - ', 'what I actually want to print out here'
Which prints something like:
filenameX.py:273 - what I actually want to print out here
To make it more simple, I want to be able to do something like:
print debuginfo(), 'what I actually want to print out here'
So I put it into a function somewhere and tried doing:
from debugutil import debuginfo
print debuginfo(), 'what I actually want to print out here'
print debuginfo(), 'and something else here'
Unfortunately, I get:
debugutil.py:3 - what I actually want to print out here
debugutil.py:3 - and something else here
It prints out the file name and line number on which I defined the function, instead of the line on which I call debuginfo(). This is obvious, because the code is located in the debugutil.py file.
So my question is actually: How can I get the filename and line number from which this debuginfo() function is called?
The function inspect.stack() returns a list of frame records, starting with the caller and moving out, which you can use to get the information you want:
from inspect import getframeinfo, stack
def debuginfo(message):
caller = getframeinfo(stack()[1][0])
print("%s:%d - %s" % (caller.filename, caller.lineno, message)) # python3 syntax print
def grr(arg):
debuginfo(arg) # <-- stack()[1][0] for this line
grr("aargh") # <-- stack()[2][0] for this line
Output:
example.py:8 - aargh
If you put your trace code in another function, and call that from your main code, then you need to make sure you get the stack information from the grandparent, not the parent or the trace function itself
Below is a example of 3 level deep system to further clarify what I mean. My main function calls a trace function, which calls yet another function to do the work.
######################################
import sys, os, inspect, time
time_start = 0.0 # initial start time
def trace_libary_init():
global time_start
time_start = time.time() # when the program started
def trace_library_do(relative_frame, msg=""):
global time_start
time_now = time.time()
# relative_frame is 0 for current function (this one),
# 1 for direct parent, or 2 for grand parent..
total_stack = inspect.stack() # total complete stack
total_depth = len(total_stack) # length of total stack
frameinfo = total_stack[relative_frame][0] # info on rel frame
relative_depth = total_depth - relative_frame # length of stack there
# Information on function at the relative frame number
func_name = frameinfo.f_code.co_name
filename = os.path.basename(frameinfo.f_code.co_filename)
line_number = frameinfo.f_lineno # of the call
func_firstlineno = frameinfo.f_code.co_firstlineno
fileline = "%s:%d" % (filename, line_number)
time_diff = time_now - time_start
print("%13.6f %-20s %-24s %s" % (time_diff, fileline, func_name, msg))
################################
def trace_do(msg=""):
trace_library_do(1, "trace within interface function")
trace_library_do(2, msg)
# any common tracing stuff you might want to do...
################################
def main(argc, argv):
rc=0
trace_libary_init()
for i in range(3):
trace_do("this is at step %i" %i)
time.sleep((i+1) * 0.1) # in 1/10's of a second
return rc
rc=main(sys.argv.__len__(), sys.argv)
sys.exit(rc)
This will print something like:
$ python test.py
0.000005 test.py:39 trace_do trace within interface func
0.001231 test.py:49 main this is at step 0
0.101541 test.py:39 trace_do trace within interface func
0.101900 test.py:49 main this is at step 1
0.302469 test.py:39 trace_do trace within interface func
0.302828 test.py:49 main this is at step 2
The trace_library_do() function at the top is an example of something that you can drop into a library, and then call it from other tracing functions. The relative depth value controls which entry in the python stack gets printed.
I showed pulling out a few other interesting values in that function, like the line number of start of the function, the total stack depth, and the full path to the file. I didn't show it, but the global and local variables in the function are also available in inspect, as well as the full stack trace to all other functions below yours. There is more than enough information with what I am showing above to make hierarchical call/return timing traces. It's actually not that much further to creating the main parts of your own source level debugger from here -- and it's all mostly just sitting there waiting to be used.
I'm sure someone will object that I'm using internal fields with data returned by the inspect structures, as there may well be access functions that do this same thing for you. But I found them in by stepping through this type of code in a python debugger, and they work at least here. I'm running python 2.7.12, your results might very if you are running a different version.
In any case, I strongly recommend that you import the inspect code into some python code of your own, and look at what it can provide you -- Especially if you can single step through your code in a good python debugger. You will learn a lot on how python works, and get to see both the benefits of the language, and what is going on behind the curtain to make that possible.
Full source level tracing with timestamps is a great way to enhance your understanding of what your code is doing, especially in more of a dynamic real time environment. The great thing about this type of trace code is that once it's written, you don't need debugger support to see it.
An update to the accepted answer using string interpolation and displaying the caller's function name.
import inspect
def debuginfo(message):
caller = inspect.getframeinfo(inspect.stack()[1][0])
print(f"{caller.filename}:{caller.function}:{caller.lineno} - {message}")
The traceprint package can now do that for you:
import traceprint
def func():
print(f'Hello from func')
func()
# File "/traceprint/examples/example.py", line 6, in <module>
# File "/traceprint/examples/example.py", line 4, in func
# Hello from func
PyCharm will automatically make the file link clickable / followable.
Install via pip install traceprint.
Just put the code you posted into a function:
from inspect import currentframe, getframeinfo
def my_custom_debuginfo(message):
print getframeinfo(currentframe()).filename + ':' + str(getframeinfo(currentframe()).lineno) + ' - ', message
and then use it as you want:
# ... some code here ...
my_custom_debuginfo('what I actually want to print out here')
# ... more code ...
I recommend you put that function in a separate module, that way you can reuse it every time you need it.
Discovered this question for a somewhat related problem, but I wanted more details re: the execution (and I didn't want to install an entire call graph package).
If you want more detailed information, you can retrieve a full traceback with the standard library module traceback, and either stash the stack object (a list of tuples) with traceback.extract_stack() or print it out with traceback.print_stack(). This was more suitable for my needs, hope it helps someone else!

Seemingly random error appearing when executing code in Python 3.3

Below is a portion of code from a program I have written that is by all means pretty basic.
pc1 = random.choice(cards)
cca1 = random.choice(cards)
while (pc1 == cca1):
cca1 = random.choice(cards)
ccb1 = random.choice(cards)
while (pc1 == ccb1) or (cca1 == ccb1):
ccb1 = random.choice(cards)
pc1, cca1 and ccb1 are just names of variables, shortened for ease of use. What this portion of code does is try to take 3 entries from a dictionary named cards. It uses the while functions to ensure that the chosen cards are not the same; they will always be different.
This goes on until I have 9 unique variables from my dictionary of 52, and it works fine except for sometimes producing the following error:
Traceback (most recent call last):
File "C:\Python33\Programs\Poker\1.0.py", line 231, in <module>
ccc2 = random.choice(cards)
File "C:\Python33\lib\random.py", line 252, in choice
return seq[i]
KeyError: 0
The variable in the error above (ccc2) is just a continuation of the previously shown code, and the variable supposedly causing the error changes every time.
The error only occurs sometimes (sometimes the program runs fine, other times it shows the error), and the line it occurred on also changes with every appearance.
I understand my code is inefficient but I'm really just looking to stop this error, and maybe some useful ideas/hints on how to improve.
Once again; does what its supposed to but unidentifiably returns the error mentioned at seemingly random times with a seemingly random site of cause.
Thanks in advance!
The way random.choice works is designed for sequences, not mappings. It picks indices, so will sometimes try cards[0], which evidently isn't a valid key. The reason that the error appears random is, of course, because it depends on the value picked by random!
You can fix this by explicitly choosing from a sequence:
random.choice(list(cards))
To improve your code more generally, note that random also includes sample:
rcards = random.sample(list(cards), 3) # pick three random cards
Note that in both cases, we randomly choose keys from the dictionary.

list() gives TypeError: 'Node' object is not callable - BUT ONLY AFTER SOME CODES

I hope this is an easy one for you guys.
This is my script, for Nuke.
selNodes = nuke.selectedNodes()
for list in selNodes:
if list.Class() == 'Read':
layerArray = []
# Get the list of layers and make unique using set
for chanList in list.channels():
channelLayer = chanList.split('.')
layerArray.append(channelLayer[0])
print list(set(layerArray))
It gives an error:
Traceback (most recent call last):
File "<string>", line 11, in <module>
TypeError: 'Node' object is not callable
So I tried a simpler code of the same nature:
a = [1, 1]
print list(set(a))
And it didn't work. Same error message. Now here's the strange thing: I opened up a new Nuke and ran the simpler codes again, it worked. I couldn't understand why, but I was happy. So I put in my original codes and ran it, error message. I deleted them, the editor is now clean. And ran the simpler code again, error message!!
Which means that a working code can be rendered failure after I pasted and deleted something else!
Can anyone shed some light on this issue? Nuke is a very established software I don't know if it's a software bug.
It is because, you are using list as the loop variable, which hides the builtin function list. You are using that function in
print list(set(layerArray))
The loop variables are leaked even when the loop is over, check this program to understand better
for i in range(10):
pass
print(i)
This will print 9. It means that, i is still available in the program even after the loop is over. The case in your program, after iterating over the selNodes, list variable has the last variable. And you are trying to call that like a function when you say
print list(set(layerArray))
That's why it fails. There are two ways to fix this.
Just change the loop variable to something else.
Use del list when the loop is over. Just pretend that I didn't suggest this. This is NOT recommended. Just change the loop variable to something else.

Python Factorial Calculator Error?

So, I starting to learning Tkinter in Python, (just starting to learn Python, for that matter) and decided to try and create a factorial calculator using Tkinter. I'm not going for anything too fancy and here is what I've come up with:
from Tkinter import *
import tkMessageBox
def calculate():
number = inputNumber.get()
inputNumber.delete(0, END)
product = 1
for i in range(number):
product = product * (i+1)
inputNumber.insert(product)
cal = Tk()
cal.title("Factorial Calculator")
cal.geometry('450x300+200+200')
factorialNumber = IntVar()
inputNumber = Entry(cal, textvariable=factorialNumber)
inputNumber.pack()
enterButton= Button(cal, text="CALCULATE!", width=20,command=calculate)
enterButton.pack(side='bottom',padx=15,pady=15)
cal.mainloop()
So I ran this, and when I hit the "CALCULATE!" button, it spits out this error:
Traceback (most recent call last):
File "C:\Python27\Lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
File "C:\Users\Wesley Yu\Desktop\New folder (4)\module1.py", line 8, in calculate
for i in range(number):
TypeError: range() integer end argument expected, got str.
I've already tried fixing it, but to no avail. What should I do?
Sorry if this is very basic, still learning :)
Entries result in strings. Pass it to the int() constructor first.
>>> int('42')
42
Try this:
for i in range(int(number)):
etc
python has a built in function for calculating factorials with the math module:
import math
i = 40 #an example number
print math.factorial(i)
It looks like you are running python 3.x, so you will be unable to use the exact code above without any modifications to it. I hope this helps.

Categories