I am a beginner in Python and I am practicing functions. I am trying to run the below reproducible code in Jupyter but fetching error `TypeError: 'module' object is not callable.
Below is the reproducible code creating the function gbp_to_usd:
def gbp_to_usd(gbp):
usd = float(gbp) * 1.5
return usd
gbp = input("Enter the gbp: ")
usd = gbp_to_usd(gbp)
print("The converted gbp amount in usd is: " + str(usd))
But when I run the same code in IDE, I do not get the error:
I tried running the same code in Python IDE and replicate the error, but to my surprise, it ran successfully. So this got me confused further. I researched online and found that a missing mathematical operator could cause this error but my function here is not missing any operator (function gbp_to_usd has a basic mathematical operation).
Does anyone have any idea what could be causing this error? why can't it call str() on a float variable?
Let me know
You have ran 164 cells before the one shown in the image. One of those cells has overwritten print or str functions with a module object since those are the only 2 functions on the line that are being called
On a fresh Python environment (or a reset Jupyter kernel), you shouldn't expect that error, and thus, that is the "fix"
Related
I'm trying to make a Python script that connects me to a VPN Server 'number' (with this number as a variable)
I wrote:
import os
VPNServer = 0
VPNServer += 1
os.system("networksetup -connectpppoeservice 'VPNServer {servernumber}'").format(servernumber = str(VPNServer))
print("→ Successfully connected to", "VPNServer", VPNServer)
But everytime I try to run it, the console gives me an AttributeError
AttributeError: 'int' object has no attribute 'format'
I don't understand it because I took the string version of the variable
If someone could help, it would be super cool
I'm on macOS and I'm using Python 3.8.1
In the snippet you provided, you write
os.system('<some string>').format(args)
You are making the format call on the return value of os.system, which happens to be an integer. This is identical to writing e.g.
5.format(args)
Since int objects have no attribute format, you get the AttributeError you described.
What you want to write is
os.system('<some string>'.format(args))
In this specific case, your snippet should resemble
os.system(
"networksetup -connectpppoeservice 'VPNServer {servernumber}'"
.format(servernumber=VPNServer)
)
Note that the str(VPNServer) call is superfluous, since format will autmatically call the __str__ method of the object provided.
First off my apologies if this is a noob issue but I am completely new to coding.
I am following a guide on building a harmonic oscilation simulator in Vpython but every time I try to run the program I get this error.
File "C:/Users/Nathan/Desktop/vspring", line 14
F_spring=-k*(block.pos -L)
TypeError: bad operand type for unary -: 'tuple'
Can someone please look at the code and tell me where I have gone wrong. I have literally followed the guide to the letter as I am just copying line by line what it tells me to put. But it has no trouble shooting. It just states "Your program is now ready to run"
from visual import *
from visual.graph import *
L=vector(1,0,0)
s=vector(1.,0,0)
block=sphere(radius=0.25, color=color.cyan, pos=L+s)
spring=cylinder(pos=(0,0,0), axis=L+s, radius=.1)
scene.autoscale=0
posgraph=gcurve(color=color.green)
k=100,; g=9.8; mu_s=.5; mu_k=.5; m=1.; dt=.0001; d=0.; t=0.
F_mu_s=mu_s*m*g
F_spring=-k*(block.pos-L)
almost_zero_p=mag(F_spring)*dt
block.p=vector(0,0,0)
while not(mag(block.p)<almost_zero_p and F_mu_s>=mag(F_spring)):
if not(mag(block.p)<almost_zero_p):
F_mu_k=m*g*mu_k*norm(block.p)
else:
F_ms_k=vector(0,0,0)
F_spring=-k*(block.pos-L)
Fnet=F_spring+F_mu_k
block.p=block.p+Fnet*dt
block.pos=block.pos+block.p/m*dt
spring.axis=block.pos-spring.pos
d=d+mag(block.p/m)*dt
posgraph.plot(pos=(t,block.pos.x-L.x))
t=t+dt
print "Total distance traveled is", d
When you do at line 12 :
k=100,
You actually create a tuple (100,).
If you wanted to create a float, do :
k=100.
Indeed when you were trying to do -k, the unary operator - doesn't work because of k type (tuple). I think this is just a syntax mistake.
I tried writing the code for a problem, but the module won't run. It says invalid syntax, but it's not highlighting anything.
The code: http://pastebin.com/cJVNBcYE
The problem: http://pastebin.com/p8E0E0Nj
I don't understand why it's not working.
I have numDealers set as a variable so that info can be entered in the program. The arrays are all defined. I have index=0 and x=1 to set up the loop for the numDealer arrays for sales and commission. I have another array=index section to calculate commissions. And then I have the prints set up.
Why isn't the program working? I don't understand.
Please post code in future, with a full traceback of the error. However:
else print(sales[index]) and print(comm[index])
should be:
else:
print(sales[index]) and print(comm[index])
i.e. you are missing a colon
I'm a bit puzzled by the and. It means that the second print will only be executed if the first fails (unlikely). Did you mean:
else:
print(sales[index])
print(comm[index])
?
By the way, it appears you are not using arrays but lists. The Python standard library includes a module called array https://docs.python.org/3/library/array.html which you do not appear to be using. So don't have a list called array, that collides with the standard library module name, and can cause no end of confusion.
I'm getting started with Codechef. I submitted following code in python to solve this question.
The code below works fine with codechef's online (python) IDE as well as my local IDE. But, when I submit it on codechef, it results in Runtime Error (NZEC). Can someone explain to me the reason behind this?
withdrawCash = int(input())
totalCash = int(input())
if withdrawCash < totalCash and withdrawCash % 5 is 0:
totalCash = (totalCash - withdrawCash) - 0.5
print(totalCash)
The problem supplies both the inputs in a single line. Your code waits for input in 2 lines. Change it to:
withdrawCash,totalCash = map(int,raw_input.split())
NZEC (Non Zero Exit Code) occurs when your code doesn't return zero on exit. It can happen due to a number of reasons, but if splitting the input doesn't solve the problem, add an exception for EOFerror. Just write:
try:
withdrawCash = int(input().split()) # raw_input in the case of Python 2
except EOFerror:
print ("exit") # this is jst a pseudo statement `
Use indentation properly. I am currently using an android app of stack exchange in which writing code is not so easy. codechef is pretty poor when it comes to Python. Switch to any other lang for CP.
Try directly submitting the code without running it on Codechef ide because it with me also it showed the same but when I submitted directly I got submitted successfully. so Directly submit your code.
iam trying to use the math.trunc in Blender 2.49b Python
but iam getting this error
AttributeError: 'module' object has no attribute 'trunc'
i also imported math
its on line
uv[i][0] = trunc(uv[i][0] * 100000) / 100000
i also tryied it via the int, like
uv[i][0] = int(uv[i][0] * 100000) / 100000
which gives me an error
TypeError: 'float' object is
unsubscriptable
so how should i trunc the value:(
thank you
The second error seems to imply that uv in your code is a float object and you are trying to subscript it uv[i]. Try to math.trunc(uv) and see. Also you can check if trunc is available by doing hasattr(math,'trunc')
It might depend what verson of Python is used by Blender (I imagine that would be Python 2.5).
Try this in Blender:
import math
help(math)
This will crash Blender, but you will be able to see the math to the library under FILE and you should be able to scroll down to see if the trunc function is available in the version of Python used by Blender. It might be not present, which would explain the error.