Module doctest does not run [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 days ago.
Improve this question
I am trying to use the doctest module to test code. I tried this example:
import doctest
def areaTriangulo(base, altura):
return 'El area del triangulo es: '+str((base*altura)/2)
"""
funcion que nos devuelve el area de un triangulo
>>> areaTriangulo(4,5)
'El area del triangulo es: 20.0'
"""
doctest.testmod()
The test has a wrong answer on purpose, but the test tells me that there are no mistakes. Why?

Make sure the docstring is at the top of the function definition; not at the bottom; otherwise Python won't recognise it as a docstring:
def areaTriangulo(base, altura):
"""
funcion que nos devuelve el area de un triangulo
>>> areaTriangulo(4,5)
'El area del triangulo es: 20.0'
"""
return 'El area del triangulo es: '+str((base*altura)/2)

Related

Python code for Debye theory gives a syntax error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have this code that I typed into Python and I'm getting "Syntax error: invalid syntax" for b=Od/T. Does it have to do with how it's defined? How can I fix it
import scipy.integrate as sci
import scipy.constant as scc
import math
import numpy as np
import matplotlib.pyplot as plt
from IPython import get_ipython
get_ipython().run_line_magic('matplotlib', 'inline')
def f(T):
n=6.022*(10**28)
Od=429
V=10**(-3)
ft=lambda x: ((x**4)*math.exp(x)/(((math.exp(x))-1**2))
b = Od/T
a=0
C=9*V*n((T/Od)**3)*scc.k*(sci.quad(ft,a, b.any(),limit=10))[0]
return C
T1=np.arange(5,500,1)
plt.plot(T1,f(T1),'r-')
You're missing a closing parentheses on the previous line:
ft=lambda x: ((x**4)*math.exp(x)/(((math.exp(x))-1**2))
# ^ This parenthesis is never closed.
Error in the line before that. You can try this.
fr=lambda x: (x**4)*math.exp(x)/((math.exp(x))-1**2)

Value entered in a function changed by the default value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
So I created this function:
def bs_obj(url, lan="html.parser"):
try:
html = urlopen(url)
bsObj = BeautifulSoup(html, lan)
print(lan)
return bsObj
except HTTPError as e:
print(e)
Now, if I call the function with the next code: object = bs_obj(html, "lxml"), the console prints html.parser. Same goes if the code is object = bs_obj(html, lan="lxml"). What's going on?
EDIT: (SOLVED) I'm ashamed. I was calling bs_obj(html) some lines before the codeline I used as example.
I believe you are running the wrong file. For reference.
def bs_obj(lan="html.parser"):
print(lan)
if __name__ == "__main__":
bs_obj()
bs_obj("lxml")
bs_obj(lan='html5.parser')
Correctly outputs
html.parser
lxml
html5.parser

'module' object has no attribute 'Serial'

I'm using the following code to communicate my pc with an arduino, but I get the error mentioned in the title, module objet has no attribute Serial.
#!/usr/bin/python
# Importamos la libreira de PySerial
import serial
# Abrimos el puerto del arduino a 9600
PuertoSerie = serial.Serial('/dev/ttyACM0', 9600)
# Creamos un buble sin fin
while True:
# leemos hasta que encontarmos el final de linea
sArduino = PuertoSerie.readline()
# Mostramos el valor leido y eliminamos el salto de linea del final
print "Valor Arduino: " + sArduino.rstrip('\n')
The curios thing is the the code used to work, but then I installed matplotlib and drawnow libraries, I believe that has caused the problem but I don't know how to fix it, because and need those libraries any way.
The other matter is that is I copy the code line to line into the terminal it works but of course I need I with the loop in a .py file.
The solution is to not name the source file serial.py since in such case Python takes that instead of the actually desired serial module.
(Since the question was solved in the comments and no answer has been posted, inspired by a relevant meta question I'm adding this answer to make the question complete. I'm not trying to get credit for deets' solution and I'm posting it as a community wiki answer.)

Syntax error for down()? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm a beginner computer science student and using python in computer science 1. My assignment is to write a program that creates a spirograph. I think that the code is all right to do that, but when i run it, an error message pops up that says syntax error and it highlights down(), which is a common turtle command. I have no idea why. It said syntax error for main(), but then i restarted python and now it says there's an error in down(). Here's the code:
from turtle import *
from math import *
def xValue(R,r,p,t):
x=(R-r)*cos(t)-(r+p)*cos((R-r)/r*t)
def yValue(R,r,p,t):
y=(R-r)*sin(t)-(r+p)*sin((R-r)/r*t)
def initialPosistion():
t=2*pi
up()
goto(xValue(R,r,p,t),yValue(R,r,p,t)
down()
def iterating(R,r,p):
t = 2*pi
while t < 0:
t = t-0.01
goto(xValue(R,r,p,t),yValue(R,r,p,t)
up()
def main():
R = 100
r = 4
p = int(input("Enter p(10-100): "))
if p < 10 or p > 100:
input("Incorrect value for p!")
iterating(R,r,p)
input("Hit enter to close...")
main()
Missed a closing ) at the end of this line:
goto(xValue(R,r,p,t),yValue(R,r,p,t))

NameError: name 'random' is not defined [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Here is my code:
r = random.randint(1,10)
But for some reason it's giving me the error
NameError: name 'random' is not defined
Other info: Mac, Python, 3.4.0 pylauncher
You have to import the module random:
import random
r = random.randint(1,10)
# ...
>>> import random
>>> r = random.randint(1,10)
>>> r
10
you need to import the random library with the import keyword
import random
random.randint(1,10) #no. between 0 and 10

Categories

Resources