Getting errors when copying exact code from Codecademy [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
So I am completely new to any programming language and really want to learn it this time (multiple attempts) and these things aren't making it any better. I must be doing something completely simple completely wrong. shows my example.
Taking code from codecademy and putting it directly into the IDE creates errors. I am using the 2.7 interpreter as that is what codecademy uses as well. I tried indents but nothing so far. Anyone care to blow my mind on how simple a fix this is?

You are using Python 3.4. Figured it out from the interpreter selected by Eclipse. Try changing the interpreter to 2.7. It should work.

As far as I can see from the screen shot, you are trying to do:
>>> from datetime import datetime
>>> now = datetime.now()
>>> print '%s/%s/%s' % (now.year, now.month, now.day)
2015/12/24
And it actually works on my client. (Python 2.7) I cannot see your error clearly but probably it is because you are (for some reason) trying to run it with Python 3.

Related

TypeError: 'fn' must be callable (Prefect - Python) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
I'm starting to leart some prefect + python and I'm facing a strange error at the very beginning with a very easy script:
I have already tried it in two diferents PC.
It looks like you've installed Prefect 2, but are following instructions for Prefect 1.
I recommend starting with the first steps tutorial in the Prefect docs: https://docs.prefect.io/tutorials/first-steps/
the way with statements work is that the variable you're defining only exists within the indented code block underneath the with statement itself. The end of your code should be:
with Flow("prueba 1") as flow:
carga()
flow.run()

Python code will only print first line while using input [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 11 months ago.
Improve this question
I am attempting to print the code, but only the first line shows up in the module when ran.
I tried moving lines around entirely but couldn't figure it out. The code is below
num1=input("10")
num2=input("50")
try:
print(int(num1)+int(num2))
except:
print("one of those is not a number")
It will only show 10 on the module.
You seem to be confused on how input() works. The string you provide is printed and what you type into the terminal after it is returned to the variable. If you have no way to type to the program, then input() is not what you're looking for. For more help, the docs for input() can be found here: https://docs.python.org/3/library/functions.html#input
I don't understand what kind of problem are you having here, so I will suggest a few solution to the potential problems you are trying to fix.
The other lines are not printed : looks like that you are a little bit confused about the input() method. See this to learn about : https://docs.python.org/3/library/functions.html#input
The program does not run : the program is unindented and it's mandatory to indent the code in Python, so try to give a tab in the try "body" and in the except "body".

How to visualize a tournament bracket using Python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
How can I produce a tournament bracket image, like the above, using Python? This sort of image reminds me of a dendrogram (I'm not sure if it's literally a dendrogram or not), and I know Python can produce very sophisticated dendrograms, so I feel like my simple and regular structure should be possible. My priority is for the solution to be as straightforward as possible. I don't need a highly customizable or beautiful result.
Code which produces the exact image above would be great, but also code that produces something similar would be very helpful and I can adapt it myself. Or a suggested method, with no code, would also be helpful.
I have tried using BracketMaker, which is the first hit which shows up when I try google searching this question. In a Jupyter notebook, running Python 3.7.10, I have tried the following:
!pip install BracketMaker
from bracket import bracket # from the BracketMaker documentation
and I get the error message:
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("")? (bracket.py, line 50)
As far as I can tell, this is an error from using Python 2 code? I would like code that works in Python 3.
You could look into diagrams. Its Custom class seems like it would help.
EDIT: I've updated the project to support Python3. You can clone it here. All you have to do is navigate to the directory and run $ python3 bracket/bracket.py.
I tried updating the PyPI project definition, but I can't without the author's permission.

Python help - AttributeError: 'module' object has no attribute 'ArgumentParser' [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 3 years ago.
Improve this question
I need help with something that should be basic. Using the example from Python.org I am encountering the following error
parser = argparse.ArgumentParser(description='Process some integers.')
AttributeError: 'module' object has no attribute 'ArgumentParser'
import argparse
parser = argparse.ArgumentParser()
A print argparse.__ file__ confirmed that it is calling the correct argparse.py file:
File "C:\Python27\lib\argparse.py", line 86, in
Also I made sure there are no nameclashes withother modules. Infact, just removing everything else from the script still calls this error.
Have tried reinstalling python 2.7.9 with no change.
Help this noob please!
It's because your file name is the same as the module name. Rename your file and try again.
so, I had:
from numpy import *
and I changed this to import numpy and it didn't work.
Then I copied all the test verbatim and made a new .py file and saved it out.
Now it works.
thank you Python. I think i'll stick to C!! :S
I think I know what's wrong. It may be unavailable in the release of Python 2.7 that you're using. Try installing Python 2.7.8 or 2.7.9.
When importing modules, python usually first looks for them in your current Path, and only if it can't find them there, it will then look in the other Python-paths (found in sys.path).
Like Daniel Roche said, You obviously named your script "argparse.py" (thinking this would be a good name for an example on how to use argparse, an intuitive and common mistake, I think) and then basically told your script to import itself (how rude).
... And since your script does not have an "ArgumentParser" method defined, this must of course yield an Error.

My Python code return different answers when it runs and when it is in step-by-step debugging [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I try to write some code on Python2.7, which will able to implement a bignum arithmetic, using linear lists. I know, this is useless in Python, but it's my homework in collage. I write some working pieces of code, but problem is in dividing. I'm sure that function works, but when I run code to test it, I just get wrong answer (in some cases). But if I execute code step-by-step, it works correctly.
I'm using linux, but I tested my code on my friend's windows computer, and I got the same problem. I wrote code in Eclipse with PyDev, if it is matter.
My code on Ideone: Code
If lines in console output are the same - output is correct. On Ideone output is incorrect too. But if you put a breakpoint on line 383 and then go in the _simple_div method, answer will be correct
I hope you help me to find a reason of this.
P.S. Sorry for ugly code.
If I run your code, I get
~/coding:$ python divbug2.py
1-1
10
That -1 doesn't look right. Is there a -1 being inserted somewhere in the division? First thing to try is to search for -1 in that function, which gives
i-=1
res._addFirst(i)
if i==-1: i=0
.. and that looks strange, because if i == -1, then you've just added it to res. Maybe we should check first, i.e.
i-=1
if i==-1: i=0
res._addFirst(i)
Swapping those two lines produces
~/coding:$ python divbug2.py
10
10
And then -- after writing a real .copy() method, because copy.deepcopy was really slow and even using PyPy I got bored waiting for things to finish:
>>>> all(int(str(LongNum(x)._simple_div(LongNum(y)))) == x/y for x in range(2000) for y in range(1, 2000))
True
I'm not sure why this was working for you when you did it step-by-step, but I'm a little surprised it worked at all.

Categories