Pseudocode: does this make sense? - pseudocode

Does the below make sense in pseudocode?
It's a fairly simple beginner program.
Start program
Print welcome message text main menu
Print ticket type options text main menu
Prompt user for ticket option
Prompt user for number of tickets
Prompt user for confirmation
If confirmation is not 1 prompt user to return to main menu
If confirmation equals 1 proceed next step
Compute total = ticket option type cost * number of tickets
Print confirmation total price
End Program

Yes, but try to write it a bit differently, with formatting and actual pseudo code instructions (not English language). It will show the logic better. For example
// start program
showMainMenu() // welcome message and ticket type options
var option = prompt("Enter a ticket option")
var numTickets = prompt("Enter the number of tickets")
var confirmation = prompt("Press 1 to confirm")
if (confirmation != 1) {
var backToMain = prompt("Press 1 to return to main menu")
// TODO implement this, your code misses it
} else {
// confirmation is 1 here, proceed to next step
var total = ticketCost(option) * numTickets
printf("Confirmation total price: %d", total)
}
// end program

Related

KbQueueXXX functions: which to use and where do they go in relation to the trials' loop?

I am trying to collect the participants' responses (i.e., the first key they press on the keyboard) and their reaction time (i.e., the time elapsed since the presentation of a picture and the response). I am using the KbQueueXXX functions in Psychtoolbox, but I am not sure if I am using them properly.
Thanks for your help.
Dear Psychtoolbox users,
I wrote a script to run a 2AFC task where participants must respond within a fixed deadline of 2 seconds. If participants respond earlier, then they move to the subsequent trial straight away. To collect their responses (i.e., the first key they pressed and their RT), I coded a custom function that incorporates KbQueueCheck(). I am now reviewing and debugging my script but I have some doubts about the KbQueueXXX functions. I would be grateful if you could give me some feedback.
Task script
In a mixture of code and pseudocode, here is what I am doing.
KbQueueCreate(KEYBOARD, KEYS_OF_INTEREST);
KbQueueStart(KEYBOARD);
% TRIALS' LOOP STARTS
for iTrial = 1:nTrials
KbQueueFlush(KEYBOARD);
% show stimulus and record its onset and offset times
respDeadline = stimulusOffset + 2;
collectResponse(KEYBOARD, respDeadline, stimulusOnset); % <- KbQueueCheck() here!
end
% TRIALS' LOOP ENDS
KbQueueStop(KEYBOARD);
KbQueueRelease(KEYBOARD);
Custom function
Below is my custom function collectResponse() where I embedded KbQueueCheck().
function [choice, rt, choiceTime, firstPress, keyCodes, pressed] = collectResponse(deviceIndex, untilTime, targetOnset)
%% LOOK FOR KEYPRESSES
pressed = false;
while pressed == false && GetSecs <= untilTime
[pressed, firstPress] = KbQueueCheck(deviceIndex);
end
%% PROCESS KEYPRESSES
if pressed == false % NO KEYS WERE PRESSED
keyCodes = NaN;
firstPress = NaN;
choiceTime = NaN;
choice = NaN;
rt = NaN;
else % ONE OR MORE KEYS WERE PRESSED
keyCodes = find(firstPress > 0); % all keypresses
choiceTime = min(firstPress(keyCodes)); % ts of first keypress
choice = find(firstPress == choiceTime); % first keypress
if length(choice) > 1
% handle simultaneous keypresses
choice = -999;
end
rt = choiceTime - targetOnset; % reaction time
end
end
My questions
I am not sure whether I am calling KbQueueXXX functions correctly and whether they are in the expected position.
Shall I keep KbQueueCreate()/KbQueueStart() and KbQueueStop()/KbQueueRelease() respectively before and after the trials’ loop?
Shall I rather just KbQueueStart(), KbQueueCheck(), and KbQueueStop() at each trial iteratively?
Is it OK to KbQueueFlush() at the beginning of each trial, before the presentation of a new stimulus?
Is my custom function collectResponse() fit for the purpose I described at the top of this post?
Thank you very much for your time, I look forward to knowing your thoughts.
Kind regards,
Valerio
OS: Windows 10
MATLAB: 9.10.0.1851785 (R2021a) Update 6
PTB: 3.0.18 - Flavor: beta - Corresponds to SVN Revision 13009
The original post can be found in the Psychtoolbox forum.
KbQueueXXX functions: which to use and where do they go in relation to the trials' loop?

Editing and Capturing new option from optionMenu Tkinter

I created a list of optionMenus via a for loop. I need to tie my optionMenu selection to the optionMenu it came from. The for loop, I think, is making it so I am unable to tie the two values together.
def get_selection(choice):
popUp_list.append(choice)
if num >= 1 and num <5:
for num in range(1,5):
choice = StringVar(root2)
choices = {'Application', 'File', 'Website'}
choice.set('Choose Type')
popUpMenu = OptionMenu(root2, choice, *choices, command = Controller.get_selection)
popUpMenu.grid(row=num, column=0)
I know it would be easier to hard code the option menus, but for reasons I don't want to get in to, the for loop is necessary. Not for this part specifically, but for another part of my code. It's just easier to use this example here.
When an option is selected from the menus, I am able to pull the values and that works great. However, if a user makes a first selection and then wants to update their selection, I have no way to capture that. It creates a "new" selection, not tied to the optionMenu. So the new option does not replace the old option. It just creates a new selection.
Is there a way to pass the popUpMenu number through the Controller .get_selection function? When trying to pass another variable doing something on the lines of:
def get_selection(choice, num):
popUp_list.append(choice)
print(num)
popUpMenu = OptionMenu(root2, choice, *choices, command = lambda: Controller.get_selection(choice, num))
I get the error below and I'm not able to get the option selection. I'm not sure what to put as the first value. Inputting choice does not work.
self.__callback(self.__value, *args)
TypeError: () takes 0 positional arguments but 1 was given
I was able to solve this. I updated my code to :
if num >= 1 and num <5:
for num in range(1,5):
choice = StringVar(root2)
choices = {'Application', 'File', 'Website'}
choice.set('Choose Type')
popUpMenu = OptionMenu(root2, choice, *choices, command = lambda choice = choice, num = num: (Controller.get_selection(choice, num)))
So, making your command statement like:
command = lambda choice = choice, num = num: (Controller.get_selection(choice, num)))
allows you to pass variables into your function.

Beginners Visual Basic Help: I am to create a simple "game" which prompts a user to enter a number 1-10. That number must be stored

Beginners Visual Basic Help: I am to create a simple "game" which prompts a user to enter a number 1-10. That number must be stored. Then the real game begins, Using a loop have the user try to guess the number stored. Notify if the guess is too high, too low, or correct. Continue loop until correct.
I am very stuck right now; all I have is-
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim mynumber, input As Integer
mynumber = Val(TextBox1.Text)
input = TextBox1.Text
Please Help I Know this is very simple but this class is very complicated for me.
So, to start off I'm going to use your variables and we're going to declare these so the whole of the form can access them.
Dim mynumber As Integer
Dim input As Integer
We're also going to declare a seperate variable which will count the amount of times that the user has tried to guess the number, and also the number that they are going to be guessing which in this case is declared as guessnumber
Dim counter As Integer
Dim guessnumber As Integer
You want to create a button and call it btnNewGame which will start the game playing and this is going to be the code that you'll add to that button:
Dim mynumber As Random
guessnumber = Number.Next(10) + 1
This will create the random number and have it change to a new value when the new game button is clicked, so no game is the same.
We want to initialize the counter now so that whenever a new game is started then the amount of guesses that the user has had is 0 and we also want to initialize the textbox so that it is empty when the new game button is clicked.
counter = 0
txtInput.Text = ""
So now, underneath a separate play button we want to add this code
Dim uni As Integer
uni = CInt(txtInput.Text)
counter = counter + 1
Now lets add in a 'Try' function which will run through until the user has either guessed the number or exceeded the amount of turns which is 3. You can easily alter the amount of turns.
Try
If uni = guessnumber Then
MessageBox.Show("You guessed my Number!")
ElseIf uni < guessnumber Then
MessageBox.Show("Your guess is too low")
ElseIf uni > guessnumber Then
MessageBox.Show("Your guess is too high.")
End If
Catch E1 As InvalidCastException
MessageBox.Show("Please enter a number and try again.")
End Try
Hope this helps you, please let me know how you get on!

Does my pseudocode have any issues in it?

So the problem this time around deals with charge account validation. Basically, we have to design a program in pseudocode where a user enters an account number, and the program validates it by comparing it to a list we are to put in an array. The numbers should be stored in the array, and we are required to use the sequential search algorithm to locate the number entered by the user. If the number is in the array, the program displays a message indicating it is valid. If not, it displays, invalid. There is a requirement of the main module, and the function isTicketValid. I was wondering if my pseudocode was proper. Thanks!
Constant Integer SIZE = 18
Declare String account[SIZE] = "5658845", "4520125", "7895122", 8777541", "8451277", "1302850", "8080152", "4562555", "5552012", "5050552", "7825877", "1250255", "1005231", "6545231", "3852085", "7576651", "7881200", "4581002"
Module main()
Display "Please enter your charge account number"
Input account
isValidAccount(account)
Main()
Function Integer isValidAccount(account)
Declare Boolean found
Declare Integer index
Set found = False
Set index = 0
While found == False AND index <= SIZE -1
If account[index] == account Then
Set found = True
Else
Set index = index + 1
End if
End While
If found Then
Display "Your account is valid ", index + 1
Else
Display "Your account is not valid."
End If
End Function

How to have procedural code wait for user input from a GUI before continuing?

In my program there is a complex calculation that requires the user to evaluate intermediate results. This works well in a command line application (which is what my code looks like now) because the interactive prompt halts the program execution until the user hits enter. The command line code looks something like this:
def calculate(starting):
result1 = initial_calculation(starting)
user_input1 = input("What is your choice for " + result1 + "?")
result2 = calculate1(user_input1)
user_input2 = input("What is your choice for " + result2 + "?")
result2 = calculate2(user_input2)
#...etc
I would like to provide a more intuitive interface than is possible with the command line by using a GUI and allowing the user to click on a button indicating their choice instead of typing it in. I'm picturing something like this:
def do_something(starting):
result1 = initial_calculation(starting)
#wait for user to press a button indicating their choice?
result2 = calculate1(clicked_button.user_input1)
label.text("What is your choice for " + result1 + "?")
#wait for user again
result2 = calculate2(clicked_button.user_input2)
#...etc
Is there a way I can pause the execution of the procedural code that does the calculations and then resume the code after the user clicks a button? I'm not really sure how to raise or handle events here because normally in a GUI the control callbacks are the starting point for code execution, but here, the code starts elsewhere and needs to yield to and resume from the GUI.
(If it makes a difference, I am using Python and wxPython.)
General solution:
flag = false;
your thread:
while (!flag); // wait here, consume cpu doing nothing
gui thread:
void OnInputEvent() { flag = true; }
I'm no python programmer, but in general that means you'll need to occasionally wait for the GUI thread until that thread has received input from the user.
There may well be a more succinct, pythonesque api available, but you can certainly use raw python events.

Resources