The code I had wasn't executing whatsoever. So, I tried it with a basic code:
x=10, if x==10:, print ("Hello"),
This worked. But the moment I extended it to anything else, it wouldn't run eg.:
count=0, x=10, if x==10:, count=count+1, if count == 10:, print ("Hello"),
(That had correct indents and exc. the commas.) The loop wouldn't loop.
Anyone understand why? The other queries similar to this regard a different issue. It won't run through CMDLine either. I did uninstall and reinstall it but that changed nothing.
The reason your loop won't run is because there isn't a loop. I think what you're trying to do is this.
for i in range(11):
if i == 10:
print('Hello')
With the current string of commands that you're running, count is simply being increment from 0 to 1, and since count != 10 at that point, you never see Hello.
Related
I found this code in Python which is working fine.
But what is purpose of this "if True:", because the if is always True so this code inside will run every time. Do you know in which situations we can use this?
if True:
print (type(0x1F))
print (0b010101010)
print (0o7)
Biting my teeth on a problem right now:
I would like to have a py (Slave.py - which is quite complex and already finished) start / run by another py (Master.py) in the Python IDLE several times, with Slave.py always being completely restarted. This means that all variables and imports etc. there are reinitialized with each start. Each time the Slave.py is restarted, different variable values should be transferred from the Master.py to the Slave.py (the background is that the Slave.py requires a running time of 1 to 12 hours and the AI does not remain stable over several runs with changed parameters.)
I work in WIN10 with the Python IDLE and have Python 3.8.3.
Found something that at least executes the Slave.py in the IDLE:
(What is an alternative to execfile in Python 3?)
with open ("Slave_01.py") as f:
code = compile (f.read (), "Slave_01.py", 'exec')
exec (code)
The problem remains for me to transfer variables from the Master.py to the Slave.py. I think you can write in the Master.py for example:
global_vars = {"Wert1": "Ford", "Wert2": "Mustang", "Wert3": 1964}
local_vars = {"Wert1": "VW", "Wert2": "Käfer", "Wert3": 1966}
with open ("Slave.py") as f:
code = compile (f.read (), "Slave.py", 'exec')
exec (code, global_vars, local_vars)
My question now is how do you resume the variable values in the Slave.py?
Thanks for suggestions, how one could specifically write that.
I have a selenium script that automates signing up on a website. During the process, I have driver.implicity_wait(60) BUT there is a segment of code where I have a try/except statement where it tries to click something but if it can't be found, it continues. The issue is that if the element isn't there to be clicked, it waits 60 seconds before doing the except part of code. Is there anyway I can have it not wait the 60 seconds before doing the except part? Here is my code:
if PROXYSTATUS==False:
driver.find_element_by_css_selector("img[title='中国大陆']").click()
else:
try:
driver.find_element_by_css_selector("img[title='中国大陆']").click()
except:
pass
In other words if a proxy is used, a pop up will occasionally display, but sometimes it won't. That's why I need the try/except.
You can use set_page_load_timeout to change the default timeout to a lower value that suits you.
You will still need to wait for some amount of time, otherwise you might simply never click on the element you are looking for, because your script will be faster than the page load.
In the try block u can lower the timeout say 10 by using driver.implicity_wait(10) or even to 0. Place this before the find element statement in the try block. Add a finally block and set this back to 60 driver.implicity_wait(60).
I am 11 years old and am keen programmer learning Python.
1) I am programming a guess the number game and when I ask the user if they want to play again, I get a semantic error (I think this is the correct way to describe it) where if I input "no", "n", "NO" or "N", the if statement is still executed, causing the loop() function to run again, after calculating scores. Take a look at the following image (sorry about the cluttered windows).
Play again error: https://i.stack.imgur.com/TsEyw.png
Here is a link to the rest of the program:
https://gist.github.com/anonymous/f9be138e07c569b8721b990293d92314 (I only have 8 reputation points) , but I am looking at just this snippet:
def play_again():
again = input("\nWould you like to play again? y/n " + prompt)
if again.upper() == "Y" or "YES":
global gu_num
percent = gu_num * 10
score = 100 - percent
highscores = [{round: (score)}]
current_score = {round: (score)}
highscores.append(current_score)
print("Lets see if you can beat your score of " + str(current_score[round]) + ".\nHere we go!")
gu_num = 0
loop()
elif again.upper() == "N" or "NO":
print("Ok then.\nThank you for playing Guess The Number and I hope to see you again soon!\nThis game was created and devoloped by Kiran Patel (me), age 12. If you liked this game or want to talk to me about -->anything<--, please do email me at kiran#inteleyes.com. It'll make me happy! Thank you for playing Guess the number with me.\n\n program was developed by Kiran Patel in 2017 ad.")
input("\n\nPress the enter key to exit")
quit()
else:
print("Sorry, I don't understand. Please try again:")
play_again()
2) I'm having a similar problem with the part of my code which starts to handle files. When I input a 'no' (same if expression) the program will execute the part of that if statement which creates the file (take a look at this image: file saving result and this image: file operations code). Note that the 'file operations code' image prints out the file-save error message because I hadn't given perms to write in prog'/files folder. I know it has tried to save the file because of the error message that was printed out (I intended the error message to be printed out).
3) Also, does anyone know what #!/usr/bin/python means? I know its hashtagged out, but I have seen it before like this and I was wondering what it means.
As always, ANY help will
be appreaciated, so please don't hesitate on adding something that is not directly relevant to the question, because I may well have missed it (unless it is completely irrelevant).
Once again, thanks in advance for your help.
From Kiran
The problem is here (and on every line that looks like it):
if again.upper() == "Y" or "YES":
Here's what you (reasonably) assume it's doing (This is how you would fix it, btw):
if (again.upper() == "Y") or (again.upper() == "YES"):
Here's what it's actually doing:
if (again.upper() == "Y") or "YES":
So let's say that you typed Q at the prompt. As Python reads along the line, it sees the first comparison operator, ==, and compares just the two things on either side of it: "Q" and "Y". "Those are not equal", thinks Python, and moves on to the next part: comparing False - the answer to the first part - with "YES".
In Python, if something exists and isn't False or 0 or something similar, it gets treated as True. "False is False, but "YES" is a perfectly valid string", thinks Python. "and the or operator means that if either of these two things is True, the whole thing is True, so this must all be true and I should run this block of code now." And so it does, every time, no matter what you type.
As a general rule, when you're doing these kinds of tests, it's only safe to do one comparison at a time. Any more than that and it's time to bring in the parentheses.
Also, regarding #!/usr/bin/python: the #! is called a Shebang, and it's not part of Python at all - it's part of your operating system (assuming your operating system isn't Windows). It says to your OS: "This may look like an ordinary text file, but it's actually a script, which you should run using the program located at /usr/bin/python".
Being a beginner with Python (Python 3.x), I was trying and getting my hands dirty with the while loop, when I ran into a small snag - no doubt due to my inexperience with the language - the code runs in a continuous loop!
The code is as follows :
n=0
while (n<len(txt)) :
while (n<5) :
#t = txt[n].value
#print(t)
n=n+1
print(n)
In the program, n is a control variable. txt is a list which stores the values extracted from my excel sheet. The aim of the program is to loop through the contents of each cell in the column of the sheet (represented by the list txt).
However, due to some silly mistake in this code, Python seems to run into an endless loop. Any help will be greatly appreciated.
P.S. the 5 in the inner loop is just representative of a number I will loop through again.
This seems to work perfectly fine now :
n=0
while (n<len(txt)) :
if n==50:
break
while (n<50) :
#t = txt[n].value
#print(t)
n=n+1
print(n)
Thanks ybdesire! (y)
One small question thought - why does the Spyder editor not return back to the next console command line, some times, despite having finished execution ?