Problems with nested "IF" condition in Lua (Love2D) - if-statement

Whenever I hit space bar I get this error: "Attempt to call field "resume" a nil value."
The intention is to the hit space bar once to stop the audio, and hit it again to resume.
if(key == "space") then
love.audio.pause()
paused = true
end
if paused == true then
if (key == "space") then
love.audio.resume()
end
end
Things I tried:
Changing "==" to "=" and vice versa;
Using "and" to avoid the nested "if" statement.
Documentation (if needed): http://love2d-community.github.io/love-api/#audio_resume
Any help is appreciated, and thank you for your time.

if key == "space" then
if paused then
love.audio.resume()
else
love.audio.pause()
end
paused = not paused
end
But love.audio.resume was removed in LÖVE 11.
Use love.audio.play instead.

Related

If else condition, that else condition should fail instead of pass in postman

I want to write a if else condition so that if the value doesn't matches then it will go to else condition and i'm able to get that.
But the thing the test case is getting passed after going to else condition, but my requirement is that if the flow reaches else condition the test case should fail.
Can anyone help me in this.
I don't know anything about the context here so this is just a simple example of a test that fails if it goes down a certain path:
let a = 124
pm.test("Check that the value matches", () => {
if (a === 123) {
pm.expect(a).to.equal(123)
}
else {
pm.expect.fail(`Value didn't equal 123 - Value was ${a}`)
}
})
I created a variable a with the value 124 - The if condition is saying if a equals 123 go down this path and assert against that value. If it's anything else, it will show a fail using pm.expect.fail().
Without knowing more about what you're trying to do, I can't comment about the need to have an if/else to fail a Test. If the assertion or pm.expect() statement is not met, it's going to fail the test for you and show the reason why in the Test Results:
let a = 124
pm.test("Check that the value matches", () => {
pm.expect(a).to.equal(123)
})

Kivy remove widget

I'm having trouble removing a widgets with a label once it's added
Here's the relevant piece of code:
logi= True
if data == []:
logn =Label(text= "Incorrect Username",color=(190,0,0,1),
pos_hint={"right":1.035,"top":1.14})
self.add_widget(logn)
logu =Label(text= "Incorrect Password",color=(190,0,0,1),
pos_hint={"right":1.035,"top":1.04})
self.add_widget(logu)
logi= False
if logi == True:
textinput.text=''
textinput2.text=''
if 'logn' in locals() and 'logu' in locals() :
self.remove_widget(logn)
self.remove_widget(logu)
once the widgets have been added I can't seem to remove them, if i remove the if 'logn' in locals() and 'logu' in locals() :statement I get an error "Local variable referenced before assignment " every time I test this without the above mentioned if statment I make sure the widgets have been added
I assume you are entering this method twice (1st data==[] 2nd time data=[...]). So You should keep your variables at hand (put them on the instance - self)
logi= True
if data == []:
self.logn =Label(text= "Incorrect Username",color=(190,0,0,1),
pos_hint={"right":1.035,"top":1.14})
self.add_widget(self.logn)
self.logu =Label(text= "Incorrect Password",color=(190,0,0,1),
pos_hint={"right":1.035,"top":1.04})
self.add_widget(self.logu)
logi= False
if logi == True:
textinput.text=''
textinput2.text=''
if hasattr(self, 'logn'): #check that we put something here before...
self.remove_widget(self.logn)
self.remove_widget(self.logu)
Note all the places I've added self ...

Tkinter GUI for hangman game

I am trying to code a simple hangman game in python with a gui using tkinter. I have the gui frame set up with a user entry. The problem is when I enter a letter then run the code by pressing the play button it runs the letter through the code until the code runs out of lives (10 times). How can i improve my code so a letter is only run through once then a new letter can be input to guess again?
thank you
from numpy import*
from Tkinter import*
#set up gui frame
win=Tk() #create window assigned to variable win
welc=Label(win,text="Welcome to Hangman!").grid(row=0)
inst=Message(win,text="instructions: To play start by guessing a letter in the secret word. if the letter is in the word it will fill in a blank if not you lose a life. Continue guessing untill you have guessed the secret word.").grid(row=1)
guess_text=Label(win,text="Enter a letter or Word").grid(row=2)
blank_text=Label(win,text="Secret Word").grid(row=3)
lives_text=Label(win,text="Lives Remaining").grid(row=4)
e=Entry(win)
e.grid(row=2,column=1)
#library of secret words
lib=['hanakah','christmas','holly','thanksgiving','reigndeer','family','presents','santa','kwanza', 'chocolate', 'cheesecake']
n=len(lib)
#randomly pick secret word
inx=random.randint(1,n)
secret=lib[inx]
#set up game
lives_remaining=10
guessed_letters=''
#define function to play game
def play():
word=secret
while True:
guess=get_guess(word)
print guess
print type(guess)
if process_guess(guess,word):
Label(win,text="You Win!").grid(row=6)
break
if lives_remaining==0:
Label(win,text="You Lose!").grid(row=6)
Label(win,text="The secret word was: "+word).grid(row=7)
break
Button(win,text=("Play"),command=play).grid(row=5,column=0)
def get_guess(word):
blanks(word)
guess=e.get()
return guess
#diplay guessed letter in postion of word
def blanks(word):
Label(win,text=lives_remaining).grid(row=4,column=1)
display_word=''
for letter in word:
if guessed_letters.find(letter)> -1:
#LETTER found
display_word=display_word+letter
else:
#letter not found
display_word=display_word+'-'
Label(win,text=display_word).grid(row=3,column=1)
def process_guess(guess,word):
if len(guess)>1 and len(guess)==len(word):
return whole_word_guess(guess, word)
else:
return single_letter_guess(guess, word)
def whole_word_guess(guess, word):
if guess.lower() == word.lower():
return True
else:
lives_remaining=lives_remaining+(-1)
return False
def single_letter_guess(guess, word):
global guessed_letters
global lives_remaining
if word.find(guess) == -1:
# letter guess was incorrect
lives_remaining = lives_remaining+(-1)
guessed_letters = guessed_letters + guess.lower()
if all_letters_guessed(word):
return True
return False
def all_letters_guessed(word):
for letter in word:
if guessed_letters.find(letter.lower()) == -1:
return False
return True
mainloop()
I think I understand now.
The get_guess function is always pulling whatever info is in the e Entry, it's not waiting for the value of e to change.
A simple fix without creating listeners would be to create a new global variable called last_guess which can be initialized to '' and then passsed to get_guess as a 2nd arg to check if the guess has changed:
def get_guess(word, last_guess):
blanks(word)
guess=e.get()
#If the guess has not changed ret false
if guess == last_guess:
return False
#If the guess has changed update the last_guess
else:
last_guess = guess
return guess
And then in the main While loop:
while True:
guess=get_guess(word)
print guess
print type(guess)
if not guess: #<-- If the get_guess returned False
continue #<-- Just go on to on to the next iteration of the loop w/out updating lives etc
if process_guess(guess,word):
Label(win,text="You Win!").grid(row=6)
break
if lives_remaining==0:
Label(win,text="You Lose!").grid(row=6)
Label(win,text="The secret word was: "+word).grid(row=7)
break
I haven't run this code so it might have errors, but I think the general idea will work

Lines in my code being skipped and I am not sure why

I am using python 2.7 and I am pretty new to python. I was wanted to ask why lines in my code are being skipped although I don't see a reason for them to be.
My code is seen below:
def add_client:
code for adding client
def check_clients:
code for listing out client info
modes = {'add': add_client, 'check': check_clients}
while True:
while True:
action = raw_input('Input Action: \n').lower()
if action in modes or ['exit']:
break
print 'Actions available:',
for i in modes:
print i.title() + ',',
print 'Exit'
if action in modes:
modes[mode](wb)
if action == 'exit':
break
When I run the code and input an action that is not in the list of modes, it does not print out 'Actions available: add, check, exit' and just seems to skip like seen below.
Input Action:
k
Input Action:
If I change the code to what is seen below it works as intended:
modes = {'add': add_entries, 'check': check_stats}
while True:
while True:
mode = raw_input('Input Action: \n').lower()
if mode not in modes:
print 'Actions available:',
for i in modes:
print i.title() + ',',
print 'End/Exit'
if mode in modes or ['end', 'exit']:
break
if mode in modes:
modes[mode](wb)
if mode in ['end', 'exit']:
break
Output:
Input Action:
k
Actions available: Add, Check, End/Exit
From my understanding, I thought that when an if statement is false, the code within the if statement is skipped and the code following should be ran, but for some reason, this doesn't seem to be the case here. Is there a reason for this or is my understanding of the if statement incorrect?
The condition action in modes or ['exit'] evaluates to True regardless of the value of action. It read like (action in modes) or (['exit']) (so you apply or operator to operands action in modes and ['exit']). Non-empty list ['exit'] evaluates to True in boolean context, so or returns True. You are suggested to use action in modes or action == 'exit' here to achieve your goal.

Aborting a process in Ada that uses a protected object that requeues

I'm experiencing some troubles with my program.
I have a process that calls a function (Take_Job) that is supposed to remain blocked until a time (MINIMUM_WAIT) passes. If it doesn't happen that way, a message informing of this situation will appear.
for Printer_Id in Type_Printer_Id loop
select
delay MINIMUM_WAIT
Pragma_Assert (True, "");
then abort
Take_Job (Controller,
Printer_Id,
Max_Tonner,
Job,
Change_Tonner);
Pragma_Assert
(False,
"Testing of Take_Job hasn't been successful. It should have remained blocked.");
end select;
end loop;
The function Take_Job calls to an entry in a protected object:
procedure Take_Job (R : in out Controller_Type;
Printer : in Type_Printer_Id;
Siz : in Typo_Volume;
Job : out Typo_Job;
Excep_Tonner : out Boolean) is
begin
R.Take_Job(Printer, Siz, Job, Excep_Tonner);
end Take_Job;
Where "R" is the protected object.
The following code is the entry of the protected object. Actually, the "when" condition is True because I need to check some stuff with the parameters of the entry. Since Ada doesn't allow me to do that, I copy the parameters inside the protected object and call to a "delayed entry", then in the "delayed entry" I will make sure that the condition is met before proceeding.
entry Take_Job(Printer_Id: in Type_Printer_Id; Remaining: in Type_Volume; Job: out Type_Job; exceptionTonner: out Boolean)
when True is
begin
Copy_Remaining(Printer_Id) := Remaining;
requeue Take_Job_Delayed(Printer_Id);
end Take_Job;
Let's see the "delayed entry" code:
entry Take_Job_Delayed(for J in Type_Printer_Id)(Printer_Id: in Type_Printer_Id; Remaining: in Type_Volume; Job: out Type_Job; exceptionTonner: out Boolean)
when False is -- I've done this on purpose
begin
null; -- Actually, there would be a lot of code here
end Take_Job_Delayed;
Let's say that my goal is to pass the MINIMUM_WAIT and run the "Pragma_Assert(True, "")". If I put the "when" condition of Take_Job to "False", then everything works fine. Take_Job is never accepted and the Pragma_Assert will be executed. If I set it to "True" and the "when" condition of Take_Job_Delayed to "False", I don't get the same effect and the process gets blocked and neither of the Pragma_Asserts will be executed.
Why? It looks like the problem is in the "requeue" or somewhere near that, but why is this happening?
You need to do the requeue with abort;
entry Take_Job(Printer_Id: in Type_Printer_Id;
Remaining: in Type_Volume;
Job: out Type_Job;
exceptionTonner: out Boolean)
when True is
begin
Copy_Remaining(Printer_Id) := Remaining;
requeue Take_Job_Delayed(Printer_Id) with abort;
end Take_Job;
because otherwise the opportunity to abort the entry call has been lost. There are details in ARM 9.5.4, and a more understandable explanation in Burns & Wellings, “Concurrency in Ada”.