Can I paste a long code in a loop in python - python

if you have a 200 line code and you want to paste in a loop, how would you do it, without ruining the loop
i tried this:
code = [
your 200 line code
]
while True:
if example = example
code
else:
exit()
and expected to work. Help please

Just highlight everything you want to be in the loop and press tab (adding indent)

Related

I am trying to use pyautogui in a loop to spam my friend's dm's (don't ask why)

I have recently installed pyautogui and it all seems to be working perfectly. I want to spam my friend's dm's and therefore use command, a, c and then v. It all works completely fine if I type: pag.hotkey('command','v') many times to make it make a big message. However, I tried using for loop and also tried while loop but it does not seem to execute anything inside the loop. This means it will select the text I have written to be spammed, copies it but as the paste line is in the for loop it does not execute and therefore does not paste it over and over again. All loops seem to not work when I use pyautogui. I use PyCharm by the way.
pag.typewrite("jelly",0.1) #enters the text
pag.hotkey('command','a') #selects the text
pag.hotkey('command','c') # copies the text
for i in range(10): #should iterate the line indented 10 times
pag.hotkey('command', 'v') #pastes the copied text
pag.keyDown('enter') #presses down on enter
pag.keyUp('enter')#releases enter key
#should have sent the phrase 'jelly' ten times in one big text```
**I forgot to include import pyautogui as pag at the top because I accidentally cut it off when I copied and pasted my code**
Someone please help me find a way to use loops with pyautogui, it is very annoying.
pag.typewrite("jelly",0.1) #enters the text
for i in range(10): #should iterate the line indented 10 times
pag.typewrite("jelly",0.1) #pastes the copied text
pag.keyDown('enter') #presses down on enter
pag.keyUp('enter')#releases enter key
Why not just type the phrase jelly 10 times? I see no difference.

How to change Pycharm auto-format?

I am using pycharm as my editor in python. One problem I have with it is that whenever I press autoformat (command+option+l), it causes some of my code lines to be broken into several lines. For example, this code:
percentage_optimal_arm = self.main_program.compute_percentage_optimal_arm(algorithm_instance.chosen_arm_history,self.k_value_of_arms_list)
breaks to this:
percentage_optimal_arm = self.main_program.compute_percentage_optimal_arm(
algorithm_instance.chosen_arm_history, self.k_value_of_arms_list)
This is really annoying as my screen is big enough to include the whole line and it makes my whole code looks ugly. Is there a way to increase the limit for breaking lines in pycharm?
Change Hard Wrap value at Settings > Editor > Code Style > Python
Default is 120 line spaces. Change it your desired value ex: 180

Having trouble playing music using IPython

I have the lines of code
import IPython
IPython.display.Audio(url="http://www.1happybirthday.com/PlaySong/Anna",embed=True,autoplay=True)
And I'm not really sure what's wrong. I am using try.jupyter.org to run my code, and this is within if statements. The notebook is also taking in user inputs and printing outputs. It gives no error, but just doesn't show up/start playing. I'm not really sure what's wrong.
Any help would be appreciated. Thanks!
First you should try it without the if statement. Just the two lines you mention above. This will still not work, because your URL does point to an HTML page instead of a sound file. In your case the correct URL would be 'https://s3-us-west-2.amazonaws.com/1hbcf/Anna.mp3'.
The Audio object which you are creating, will only be displayed if it is the last statement in a notebook cell. See my Python intro for details. If you want to use it within an if clause, you can use IPython.display.display() like this:
url = 'https://s3-us-west-2.amazonaws.com/1hbcf/Anna.mp3'
if 3 < 5:
IPython.display.display(IPython.display.Audio(url=url, autoplay=True))
else:
print('Hello!')

Populate wxChoice at Start of Program Run | Python

As soon as my program is run, I want my wxChoice to be populated with items from a list I designate. I am using wxFormBuilder to handle the GUI elements of my program.
My code:
def onDropDownSelection(self, parent):
#Open designated file
lines = tuple(open("/Users/it/Desktop/Classbook/masterClassList.txt", 'r'))
#Strips the first line of the file, splits the elements, assigns to "one"
lines[1].rstrip()
one = lines[1].split("|")
#My attempt to populate the wxChoice with my list "one"
self.firstChoice.SetItems(one)
This event is activated when the user clicks on the drop-down (wxChoice) menu, and re-populates every time it is clicked on.
Is there a way I can populate my wxChoice, only once, upon the initial opening/running of the program?
I have placed this code where the wxChoice is being created. However, I am now experiencing a "Unindent does not match any outer indentation level" on line 44. How do I fix this?
Check for your indentation. Some times if you copy paste, this can mess things up.
Just rewrite it or replace it with another statement. See here:
IndentationError: unindent does not match any outer indentation level
Problem is if you make your indentation with tabs and then copy-paste some code from an example page, where the indentation is made with spaces. Then you have mixed Indentations. I've had these a lot of times.

Writing to a File with Python -- ''While not done:" Confusing Me

I have less than a year of programming experience. While learning about reading and writing files I came across this tutorial: http://www.penzilla.net/tutorials/python/fileio/
The tutorial offers the following example as a simple script to create and write to a file:
# Let's create a file and write it to disk.
filename = "test.dat"
# Let's create some data:
done = 0
namelist = []
while not done:
name = raw_input("Enter a name:")
if type(name) == type(""):
namelist.append(name)
else:
break
# Create a file object:
# in "write" mode
FILE = open(filename,"w")
# Write all the lines at once:
FILE.writelines(namelist)
# Alternatively write them one by one:
for name in namelist:
FILE.write(name)
FILE.close()
I copied this code and ran it through a Python 2.7.3 Shell. I am prompted repeatedly to enter strings which are appended to a list that will be written to a file (this makes sense to me). What I don't understand is the condition for exiting the While loop ("While not done:"). I thought this meant that I type done at the prompt to exit the loop and subsequently write the file, but done has no effect. Then I thought that any non-string entered at the prompt should break the loop and write the file. I couldn't get the loop to break at all; for anything I entered at the prompt, I was just prompted again with "Enter a name:".
By removing the While loop and retaining the if/else statement, I got the code to work for a single prompt. Can someone tell me what I am not understanding here? I am guessing it is a fairly simple concept that wasn't explained in the tutorial because it was assumed to be obvious. Since "done" is such a common word, I wasn't able to find any Python specific meanings for it.
I would stop following that tutorial right now. The code isn't Pythonic, it's way too complicated, and it seems to be pretty outdated.
That being said, here's how I'd write that tutorial's code (yes, it does the same thing, but only the right way):
with open('test.dat', 'w') as handle:
while True:
name = raw_input('Enter a name: ')
if not name:
break
handle.write(name + '\n')
done is assigned once on line 3:
done = 0
Therefore, this the while loop will continue to loop as long as done is still "not 0":
while not done:
I.e. it will continue to loop forever, unless it hits a break statement (line 11). Unfortunately, the code is flawed and that will never happen.
If you want to stop when you type 'done', then change the if statement to:
if name == "done":
But, be aware that the literal string done above has nothing to do with the variable done assigned earlier.
It's not your fault. That code provides no way to break out of the loop.
if name == 'end':
break
The code is bad, firstly.
In this case, done is the name of a variable. As written, it will loop forever since there's no way to exit.
You should stop following that tutorial and pick a better one: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers

Categories