I don't understand why the top variable in the append function is greyed out.
No matter what I do, the value of top stays None.
This is the output. I also tried using 0 instead of None and I still have the same problem. I've included the code below.
top = None
stack = []
def append(a, top):
stack.append(a)
if top == None:
top = 1
else:
top += 1
def pop(top):
if top == None:
print('underflow error encountered')
else:
print('the popped element is:', poppedele)
print(top)
while True:
kk = input('wat do you want to do ....')
if kk == '1':
a = input('enter the element to append')
append(a, top)
print(stack, top)
elif:
pop(top)
print(stack, top)
else:
break
Here's a screenshot of what it looks like:
Thanks #aarni joensuu.
Turns out I should add global top in those functions only then the change will be made to the overall variable.
Related
I used this program for few weeks and it worked perfectly. Now, without changing anything, it doesn't work. More precisely, the orientation value doesn't change when you click on the radiobutton. Then, the program PosChoisie return 0 instead of either 1 or 2.
I'm on this program for 2 days, but even if I tried hard, I didn't found the error. Thanks for your futur help.
value=StringVar()
orientation = IntVar()
radiobutton_Horizontal = Radiobutton(fen, text="Horizontal", variable=orientation, value=1).pack()
radiobutton_Vertical = Radiobutton(fen, text="Vertical", variable=orientation, value=2).pack()
def PosChoisies():
L=[]
ori=IntVar()
ori = orientation.get()
if ori == 1:
o='horizontal'
L.append(o)
#print('horizontal')
elif ori == 2:
o='vertical'
L.append(o)
#print('vertical')
print(L)
print(ori)
return L
butValider = Button(fen, text="valider", relief = RAISED, command=lambda:(print(abs,ord,o)).pack()
fen.mainloop() ```
I'm just stuck with some loop situation.
I was working with selenium, so... if it loops in a wrong place, usually it occurs an error with StaleElementReference or NoSuchElement error. And that's what I'm getting now.
A part of my code which has the problem is like this( I'll summarize it for you):
cc = 0
While True:
print(cc)
cc += 1
if A == something:
try:
B
except:
C
elif A == something_else:
try:
E
except:
F
#This is the problematic part
else:
answer = Success(z,name)
print('check')
if answer == 1:
break
elif answer == 2:
print("flag1")
shutil.move("\\Party_People\\HTML\\"+name+".html", "\\Party_People\\Misc")
driver.get('http://cdb.chosun.com/search/db-people/i_common/index.jsp')
print("flag2")
time.sleep(0.7)
driver.find_element_by_xpath("//input[#title='이름']").send_keys(name)
time.sleep(0.3)
driver.find_element_by_xpath("//button[#class='more2']").click()
time.sleep(0.3)
page = driver.find_elements_by_xpath("//div[#id='bodyarea']/div[#class='people2']/li")
print("flag3")
return 777
def Success(num, name):
time.sleep(2)
Download_html(name)
print("다운로드한 HTML을 표시중입니다.")
driver2.get(os.getcwd()+"\\Party_People\\HTML\\"+name+".html")
print("네이버 인물검색에서 해당 인물 검색 결과를 표시중입니다.")
driver3.get("https://people.search.naver.com/search.naver?sm=sbx_hty&where=nexearch&ie=utf8&query="+name+"&x=0&y=0")
Final_Choice = input("해당 다운로드 결과가 맞다면 '1'을, 아니라면 '2'를 입력해주십시오.")
return Final_Choice
And what happens is... after I input "2" for the Final_Choice which would be "answer", suddenly it just goes back and starts from the top. So "cc" prints "0" first, and then after I input "2", I got print of 'check' and then suddenly cc prints "1". Which means I didn't get any response from
if answer == 1:
break
elif answer == 2:
print("flag1")
shutil.move("\\Party_People\\HTML\\"+name+".html", "\\Party_People\\Misc")
driver.get('http://cdb.chosun.com/search/db-people/i_common/index.jsp')
print("flag2")
time.sleep(0.7)
driver.find_element_by_xpath("//input[#title='이름']").send_keys(name)
time.sleep(0.3)
driver.find_element_by_xpath("//button[#class='more2']").click()
time.sleep(0.3)
page = driver.find_elements_by_xpath("//div[#id='bodyarea']/div[#class='people2']/li")
print("flag3")
return 777
'flag1, 2, 3' didn't show up. I wonder why it starts loop from the top at that point. I mean, isn't it supposed to start over unless I put 'continue' or just let the whole code inside of loof end? If you want to see the full code, I can share it with you but... since I'm not familiar with coding, it would be messy and clumsy to check.
Thank you in advance! Hope I got some help here as always...
So I have a currency which is increasing (that system is working fine). The first part updates the label every 100 ms. I have another button which triggers the second function which is supposed to clear the labels from the first. It sets home_status equal to 0 which should in theory run Money.place_forget() to clear the code. I have tested each part individually and it works but when I put the clears inside the elif statement it doesn't. It does not give me any errors, it just simply doesn't do anything (it does print END OF UPDATE HOME so the elif is triggered).
Any suggestions?
def updatehome(self):
print("UPDATE HOME")
global buy_button, home_status, currency
MoneyLabel = Label(self, text = "Money: ")
MoneyLabel.place(x = 5, y = 70)
Money = Label(self, text=currency)
Money.place(x = 50, y = 70)
if (home_status == 1):
self.after(100, self.updatehome)
elif (home_status == 0):
print("END OF UPDATE HOME")
Money.place_forget()
MoneyLabel.place_forget()
def clearhome(self):
print("CLEAR HOME")
global home_status
home_status = 0
you are creating ten labels every second, all stacked on top of each other, but you are only deleting the very last label you create.
I wanted to create a x-y coordinate system even though this is supposed to be a text RPG as to keep track of where everything is. So, I was experimenting on making a function and test for that function that would let the character move on a x-y grid, however, no matter what I try, I cannot make it work. Here is the code:
class Player:
def movement(charactor_movement):
proceed = 0
if charactor_movement == "left":
character.position_x = character.position_x - 1
proceed = 1
elif charactor_movement == "right":
character.position_x = character.position_x + 1
proceed = 1
elif charactor_movement == "forward":
character.position_y = character.position_y + 1
proceed = 1
elif charactor_movement == "backward" or charactor_movement == "back":
character.position_y = character.position_y - 1
proceed = 1
charactor = Player()
charactor.position_x = 0
charactor.position_y = 0
proceed = 0
while proceed == 0:
print "You are at",
print charactor.position_x,
print"x and",
print charactor.position_y,
print"y."
global charactor_movement
charactor_movement = raw_input("Where are you going?")
charactor.movement()
At this point, it does what it is supposed to do up to changing the coordinates, as it prints "You are at 0 x and 0 y" and "Where are you going?" no matter what I type. I have tried adding an else to the function which it defaulted to no matter what I typed and gave me "Sorry, I cannot understand you." Any comments on fixing or generally improving the code would be appreciated.(Note: For the testing I purposely did not add a way to exit. The class is what i need fixed.)
You are getting the same coordinates with each iteration because your values within your while loop are not changing. Incrementing character.position_x within movement will never change the value of character.position_x within your while loop, as it is outside your function's scope. You have to use the global keyword within your movement function for each variable you are changing should you want your current logic to remain the same. Additionally, why not just pass charactor_movement as a parameter to your movement function, as opposed to using global as you currently are doing.
A minimal example:
Consider the following:
def somefunct(x):
mycode = x
mycode = 'no codez'
while True:
print mycode
codez = raw_input('gimme teh codez: ')
somefunct(codez)
which outputs
>>>[evaluate untitled-1.py]
no codez
gimme teh codez: codez!
no codez
Declaring mycode as global in the function places it in the scope of the while loop when assigned, thus
def somefunct(x):
global mycode #make variable global here
mycode = x
mycode = 'no codez'
while True:
print mycode
codez = raw_input('gimme teh codez: ')
somefunct(codez)
results in the output
>>>[evaluate untitled-1.py]
no codez
gimme teh codez: codez!
codez!
I know there are a lot of these but i've been searching for an hour and nothing is working.
ship2X=eg.passwordbox("Player " + str(playerNumber) + " input the x co-ordinate for your SECOND ship ")
ship2Y=eg.passwordbox("Player " + str(playerNumber) + " input the y co-ordinate for your SECOND ship ")
return[ship2X, ship2Y]
The above code is in a function.
def haveShot(playerNumber, ship, ship2, board):
global ship2
eg.msgbox("Player " + str(playerNumber) + " your shot")
hit=False
shotX=eg.enterbox("Enter the x-coordinate for your shot: ")
shotY=eg.enterbox("Enter the y-coordinate for your shot: ")
.... error checking here....
if int(shotX) == int(ship[0]) and int(shotY) == int(ship[1]):
board[5 - int(shotY)][int(shotX) - 1] = "X"
eg.msgbox("Nice shot! You hit ship 1")
hit = True
elif int(shotX) == int(ship2[0]) and int(shotY) == int(ship2[1]):
board[5 - int(shotY)][int(shotX) - 1] = "X"
eg.msgbox("Nice shot! You hit ship 2")
hit = True
elif board[5 - int(shotY)][int(shotX) - 1] == "o":
eg.msgbox("You already tried that shot! D'oh!")
else:
board[5 - int(shotY)][int(shotX) - 1] = "o"
eg.msgbox("Unlucky - you missed!")
Ye, I have an if before that.
Then I have this near the end:
hit = False
winner = "0"
p1 = 0
p2 = 0
while hit == False:
hit = haveShot("1", player2Ship, player2Ship, player1Board)
if hit:
p1 = p1+1
hit = haveShot("2", player1Ship, player1Ship, player2Board)
if hit:
p2 = p2+2
I copied it from the first enter ship thing so I'm super confused as to why that's happening...
Any ideas?
If you want to see the full code you can see it at: http://pastebin.com/TAyHtnTs
The error I have is if I do enter the correct co-ordinate for the second ship it says I missed it, however if I enter the correct co-ordinates for the first ship it says I hit it like it shoul.
Thanks for the help you can provide :)
Couple of things:
Line 81
You can't have two subsequent return statements. The first one will exit the function. If you want to return two sets of coordinates return a nested list:
return [[x1, y1], [x2, y2]]
Line 161
Then use unpacking to get these:
p1ship1, p1ship2 = inputCoords("1")
p2ship1, p2ship2 = inputCoords("2")
Line 171/176
Make sure the two ships you're passing in are different (currently they're the same):
hit = haveShot("1", player2Ship, player2Ship, player1Board)
to
hit = haveShot("1", p2ship1, p2ship2, player1Board)
Line 170
The while hit == False condition means that the game will exit as soon as player 1's ship is hit. Use another variable to check if the game is over, such as:
while player1ShipCount > 0 and player2ShipCount > 0:
#play game
And keep track of each players' available ships.
I don't have enough reputation to comment, or I would, but...
You haven't defined "lol", you have defined "lolX" and "lolY", which are separate variables.
If you want to define a list or a dictionary with values inside, you cannot just write lolX or lolY, you would have to do (I use a dictionary here since it seems to be what you want):
lol = {}
lol[X]=eg.passwordbox("Player " + str(playerNumber) + " input the x co-ordinate for your SECOND ship ")
Then you can access the value in lol by lol[X].
You have defined separate variables, rather than define a specific value of that variable.