Can anyone help me understand the problem with this code? - python-3.x

I am using VS studios, using python. When I try to create a function, I constantly keep getting a syntax error. I am not sure why?
def sayhi():
print("Hello User")
sayhi()

Your function should have correct indentation like so:
def sayhi():
print('Hello User')
sayhi()
Otherwise python will throw a fit as indentation is very important to python and its functions, classes, etc.
The issue in your code is the space before def sayhi(): which is going to throw an IndentationError without a doubt, remove that space and you should be much better off

do it in jupyter notebook on Visual Studio and you won't have any problems. Just make sure you're on the python interpreter

Related

Issue with viewing data transformation with PyTorch

I am currently trying to process data for a simple network. This is the code I entered:
Screenshot here
I keep getting this error message but can't find any syntax problems or anyone else with this issue, I'm guessing it's something to do with my vienv because I've seen tutorials of people with no issues and that exact code. It's possible I haven't imported a package into my IDE and I am using anaconda and PyCharm if that helps.
Anyway, this is the error message I keep getting.
Error Message
You need to use transforms.ToTensor() instead of transforms.ToTensor when passing to transforms.Compose.

No module named 'line_profiler._line_profiler' when calling line_profiler

I intended to test my code with line_profiler in the following way in Pycharm but got an error just like the title said. I tried in both miniconda 3.8 and python 3.7 (Windows) but got the same error and I don't know how to fix that. I visited the github page where the source code lies and also tried to run my python file as a script. In the end, all attempts failed because of this error. It seems like a bug but I found everything is Okay in Colab. Can anyone give me any advice? Feel free to leave comments and I will appreciate that.
lp = LineProfiler()
lp_wrapper = lp(Solution) # Solution is the name of self-defined function
lp_wrapper()
lp.print_stats()

wxPython "Things are going to break" error

Ok so I'm working with wxPython with a friend of mine, now it just so happened that he implemented a new picture into the script and its working just fine for him. But if he sends the project to me I get the error:
"wx._core.wxAssertionError: C++ assertion "strcmp(setlocale(LC_ALL, NULL), "C") == 0" failed at ....\src\common\intl.cpp(1579) in wxLocale::GetInfo(): You probably called setlocale() directly instead of using wxLocale and now there is a mismatch between C/C++ and Windows locale.
Things are going to break, please only change locale by creating wxLocale objects to avoid this!"
The code in the line that breaks is the following:png2 = wx.Image("BlackBorder.png", wx.BITMAP_TYPE_ANY).ConvertToBitmap() this lies inside the __init__(self) method for wxPython
It doesnt really tell me what is wrong I feel like so I really appreciate any help.
IIRC, there are some versions of the PNG library that call setlocale in some situations. Try explicitly creating and holding a wx.Locale object at the start of your application and see if that helps.
I had exactly the same problem after updating my application from Python27 to Python3.
4 PCs worked fine but one user with French as their windows language had this error.
I solved my problem by "initialising" the wx.Locale() at the beginning of my application:
class Application(wx.Frame):
def __init__(self, parent, title):
self.locale = wx.Locale()
self.locale.Init(wx.LANGUAGE_ENGLISH)

Don't print code in Selenium HTML report

In HTML report when a test case fails I don't want the code to appear in the report, only the statements needs to be printed (example of problem). Can anyone help me out here?
I am using Python 3.6, pycharm and generating HTML reports with the help of pytest-html.
Use try and except with in your code and may be include something like below
raise NoSuchElementException("An Error has been registered")
Looking at the documentation under 'Modifying the results table'
https://github.com/pytest-dev/pytest-html
It seems like you should be able to modify the output, which also includes removing. I would suggest trying this approach because the only requirement you have is not displaying the exceptions in the HTML output.
Avoid using try-except constructions in such a way that it suppresses the stacktrace in general, as this will make debugging failed test cases harder to do.

Any tips on creating a Pygame Developer Console?

I've been getting my feet wet with Python and Pygame, and after a few bugs in a very basic game that I have been making, I've thought to myself that having some sort of console that can deal with string input (from a developer) would be very handy.
Here's an example:
I notice the player disappears after double jumping. So, in the console that I will hopefully create, I would type in:
>> report_bug("Player disappears after double jumping")
Where report_bug(string) is a user-defined function that will do the following things:
report_bug(string):
# Check if debug file already exists; create new file if none exists
# Append string to file, along with other extra info
Could someone point me in the right direction? Will I have to create something from scratch, and if so, how would I go about doing that? I've attempted to modify the source code from: http://www.pygame.org/project-pygame-console-287-.html, however it is 9 years-old and made for Python 2.x, when I'm using Python 3.4. Any help would be greatly appreciated.
I've managed to get the 2.X PygameConsole version 0.7 working, so I will stick with that!
Edit: I will attempt to port PyConsole to Python 3.X and hopefully add some more features so newcomers can easily understand what is going on.

Resources