here's my code, or rather the part of the code i'm having issues with.
the problem is at the if expression, where i'm trying to check whether a value is already in a dictionary (the actual code is in a larger while loop, used for a user interface), but when it runs the second time round it will always go into the else section, i'm assuming because of the variable name already being present in the dictionary.
Dict1 = {#empty dictionary}
completedList = (#list variable containing “true” and “false”)
name = input(“enter your name”)
score = completedList.count("true")
callback = 'cont'
while callback == 'cont':
if name not in dict1 == True:
dict1 = [name] = score
callback = ""
else:
print("that name is already in use \n please choose another")
name = input("enter a name")
I'm a student, so the simpler the code the better.
dict1 = {'er'}
completedLis=()
name = input("enter your name")
score = completedLis.count("true")
callback = 'cont'
while (callback == 'cont'):
if (name not in dict1):
dict1 = name = score
callback = ''
else:
print("that name is already in use \n please choose another")
name = input("enter a name")
I hope this solves ur problem if u type er(dict1='er')it will go to else or if u type anything else it will work
Related
I have an interesting question about referencing another variable based on another variable in an array.
Below is my code:
Dim company, price, sdev, mean, random
Dim companies
companies = Array("mm", "tgt", "boog")
mm_price = 0
mm_mean = 0
mm_sdev = 0
tgt_price = 0
tgt_mean = 0
tgt_sdev = 0
boog_price = 0
boog_mean = 0
boog_sdev = 0
For i = 1 To 3
company = companies(i)
mean = company & "_mean"
sdev = company & "_sdev"
Next i
Now, the issue occurs when I attempt to define the "mean" and "sdev" variables, as they will not use the "0" value, but instead give it the string name "mm_mean" etc. mm_mean = 0, therefore, I want mean = 0 when i = 1. Clear?
Thanks, and let me know. It is a rather strange question, and the code is cut from many different functions, so if it doesn't make sense as to why I am doing this, my apologies. I tried to make it as simple as possible so it wouldn't confuse the answerer.
This is a good example of when custom data-structures (aka "record types", "structs") should be used to group related data together. In VBA the syntax is Type:
Type Company
Name As String
Price As Currency
Mean As Double
SDev As Double
End Type
Public Sub CalculateCompanyInfo()
Dim companies(3) As Company
companies(0).Name = "mm"
companies(0).Price = 123
companies(1).Name = "tgt"
companies(1).Price = 456
companies(2).Name = "boog"
companies(2).Price = 789
For i As Integer = 0 to UBound(companies)
companies(i).Mean = ...
companies(i).SDev = ...
Next i
End Sub
Where ... means whatever custom calculation you need to do to get that value.
I have built a form with unbound fields designed so a user can input a date range, a facility name (these come from a combobox), and a badge number to generate a query in Access. I want to be able to return results within the selected date range for all facilities if the field is left blank or just the ones for a particular facility if one is selected. I also want to be able to limit the results to those that match an person's badge number.
So the possibilities I want would be:
Date Range = defined by user | Facility - All if not selected | Badge # = All if not selected
Date Range = defined by user | Facility - All if not selected | Badge # = defined by user
Date Range = defined by user | Facility - defined by user | Badge # = All if not selected
Date Range = defined by user | Facility - defined by user | Badge # = defined by user
I originally built it with just the date range and facility name and it worked fine. When I try to add in the Badge # it doesn't really work correctly.
My SQL for the WHERE TO section is:
WHERE (((Diversion.Transaction_Date) Between [Forms]![Parameters]![FromDate] And [Forms]![Parameters]![ToDate])
AND ((Diversion.Employee_Badge_Number)=[Forms]![Parameters]![BadgeNumber])
AND ((Diversion.Facility)=[Forms]![Parameters]![FacilitySelect]))
OR (((Diversion.Transaction_Date) Between [Forms]![Parameters]![FromDate] And [Forms]![Parameters]![ToDate])
AND ((Diversion.Facility)=[Forms]![Parameters]![FacilitySelect])
AND ((([Diversion].[Employee_Badge_Number]) Like [Forms]![Parameters]![BadgeNumber]) Is Null))
OR (((Diversion.Transaction_Date) Between [Forms]![Parameters]![FromDate] And [Forms]![Parameters]![ToDate])
AND ((Diversion.Employee_Badge_Number)=[Forms]![Parameters]![BadgeNumber])
AND ((([Diversion].[Facility]) Like [Forms]![Parameters]![FacilitySelect]) Is Null))
OR (((Diversion.Transaction_Date) Between [Forms]![Parameters]![FromDate] And [Forms]![Parameters]![ToDate])
AND ((([Diversion].[Employee_Badge_Number]) Like [Forms]![Parameters]![BadgeNumber]) Is Null)
AND ((([Diversion].[Facility]) Like [Forms]![Parameters]![FacilitySelect]) Is Null))
OR (((([Diversion].[Facility]) Like [Forms]![Parameters]![FacilitySelect]) Is Null));
To me, it looks like it is including the four possible results that I want to get from the form, but it isn't working right. For instance, if I leave the facility field blank, and define the badge number, it is still giving me all of the results. If I define the facility and define the badge number it does give me the correct results.
Any ideas?
This might give you some ideas building a dynamic query with multiple criteria values. In this example the user can pick any number of the criteria. It is written in VB.Net. It works with Access. I check each field to see if any criteria was provided then append it to the query if there is a valid value.
I used Interpolated strings just because it is easier to see where the spaces go. The alternative is:
String.Format("RoasterId = {0} ", itgRoaster)
I also used a String builder which is an efficient way to alter strings without the overhead of creating and disposing of them with each append. You could just use &= if this is not available in VBA.
Dim bolNeedAnd As Boolean = False
Dim sb As New Text.StringBuilder
sb.Append("SELECT Coffees.ID, Coffees.[Name], Coffees.RoasterID, Roasters.[Name], Coffees.[Type],Coffees.Rating, Coffees.Comment, Coffees.Description, Coffees.Roast, Coffees.IsExtraBold, Coffees.IsFavorite
From Coffees Inner Join Roasters on Coffees.RoasterID = Roasters.ID Where ")
If itgRoaster <> 0 Then
sb.Append($"RoasterID = {itgRoaster} ")
bolNeedAnd = True
End If
If strRoast <> "" Then
If bolNeedAnd Then
sb.Append($"AND Roast = '{strRoast}' ")
Else
sb.Append($"Roast = '{strRoast}' ")
End If
bolNeedAnd = True
End If
If strType <> "" Then
If bolNeedAnd Then
sb.Append($"AND Type = '{strType}' ")
Else
sb.Append($"Type = '{strType}' ")
End If
bolNeedAnd = True
End If
If strRating <> "" Then
If bolNeedAnd Then
sb.Append($"AND Rating = '{strRating}' ")
Else
sb.Append($"Rating = '{strRating}' ")
End If
bolNeedAnd = True
End If
If bolBold Then
If bolNeedAnd Then
sb.Append("AND IsExtraBold = 1 ")
Else
sb.Append("IsExtraBold = 1 ")
End If
bolNeedAnd = True
End If
If bolFavorite Then
If bolNeedAnd Then
sb.Append("AND IsFavorite = 1 ")
Else
sb.Append("IsFavorite = 1 ")
End If
End If
sb.Append("Order By Coffees.[Name];")
Debug.Print(sb.ToString)
Dim cmd As New OleDbCommand With {
.Connection = cn,
.CommandType = CommandType.Text,
.CommandText = sb.ToString}
Dim amountorhour As String = "A4"
If amountorhour.Substring(0, 1) = "A" Then
amount = amountorhour.Substring(1, amountorhour.Length)--**error comes here**
HrsWorked = 0
Else
HrsWorked = amountorhour.Substring(1, amountorhour.Length - 1)
amount = 0
I know this error shows up when I am asking for length of string which is out of range but in this scenario, I don't see anything like that. Please help me in figuring out the problem
The second parameter is the length of the substring from the index that you have specified. So passing amountorhour.Length works only if you pass 0 as first parameter which would return the original string.
It seems that you want to take all but the first character, you can use the overload with one parameter:
amount = amountorhour.Substring(1)
This is the same as
amount = amountorhour.Substring(1, amountorhour.Length - 1)
As an aside, you can use
If amountorhour.StartsWith("A") Then
instead of
If amountorhour.Substring(0, 1) = "A" Then
I'm trying to match two tables by string case in sensitive.
Example
[6] =
{
["itemName"] = [Ore],
["cleanName"] = [[Iron Ingot]],
["secs"] = 25004,
["gN"] = [[Luminary]],
["buyer"] = [[#Naptha42]],
["eventType"] = 15,
["timestampz"] = 1399514069,
["quantity"] = 100,
["soldAmount"] = 500,
["seller"] = [[#FataI]],
},
I'm trying to move items from the above table format into a new table, or filter them into a list.
g = e.cleanName
for k, v in pairs(savedTable.ALL_ITEMS) do searchTable[k] = v
if g == savedTable.ALL_ITEMS.cleanName then
table.insert(searchTable, savedTable.ALL_ITEMS)
end
end
The problem above is g never = that savedTable.
The table has a few thousand entries and I'm trying to display by search, I believe the best way is to create a new table with the values im looking for?
Or is there a way to use the string.match to iterate through the table and display (print) only these values.
//Update
I have an active search, each time the user enters a letter it will search the table.
Example.
Val1 = "Hello"
Val2 = "Goodbye"
Val3 = "Hi"
While searching and the user enters "H" or "h" I want the results for "hello, hi" to show up but not "goodbye"
so with the if g == string.match(savedTable.ALL_ITEMS.cleanName)
not sure if that is correct.
//UPDATE for solution of what worked (thanks ESOUI)
local searchForItem = string.lower(g)
for k, v in ipairs(myTable.ALL_SALES) do
-- if string.lower(g) == string.lower(v.cleanName) then
if string.lower(v.cleanName):find(searchForItem) then
if v.cleanName ~= nil then
table.insert(searchTable, v)
end
end
end
If ALL_ITEMS is a table that contains tables then it isn't ever going to have a cleanName field directly.
Is that == in the loop intended to be against v?
Also, if ALL_ITEMS is integer indexed you probably want ipairs instead of pairs to walk the table in order.
Why are you inserting every element into searchTable (via searchTable[k] = v) and then also attempting to insert the matching entries again (via table.insert)?
You can use string.match to match g against the cleanName in your loop if you need to but that isn't going to solve the problem if you aren't comparing what you think you are comparing.
I really do not understand why the if test always fails. I have validated the session variable sCrs_cde (course code) is correct and exists in only one of the multiple records returned by the sql query. (It is part of a foreign key tied to year and semester). I am trying to sert the value for the course title, but it is always writing out as an empty value ('')
Dim Recordset1
Dim Recordset1_cmd
Dim Recordset1_numRows
Set Recordset1_cmd = Server.CreateObject ("ADODB.Command")
Recordset1_cmd.ActiveConnection = MM_Jenz_STRING
Recordset1_cmd.CommandText = "SELECT ID_NUM, Crs_Title, YR_CDE, TRM_CDE, CRS_CDE, TRANSACTION_STS, SUBTERM_CDE FROM dbo.STUDENT_CRS_HIST WHERE ID_NUM = ? And Transaction_sts = 'C' "
Recordset1_cmd.Prepared = true
Recordset1_cmd.Parameters.Append Recordset1_cmd.CreateParameter("param1", 5, 1, -1, Recordset1__MMColParam) ' adDouble
Set Recordset1 = Recordset1_cmd.Execute
Recordset1_numRows = 0
%>
<%
Do While not Recordset1.Eof
response.write(Recordset1.Fields.Item("CRS_cde").Value)
IF (Recordset1.Fields.Item("CRS_cde").Value) = (Session("sCrs_cde")) THEN
Session("sCrs_Title") = (Recordset1.Fields.Item("CRS_Title").Value)
Session("sYr_cde") = (Recordset1.Fields.Item("YR_CDE").Value)
Session("sTrm_cde") = (Recordset1.Fields.Item("Trm_Cde").Value)
Session("sSubterm_cde") = (Recordset1.Fields.Item("Subterm_cde").Value)
EXIT Do
ELSE
Recordset1.movenext
END IF
Loop
Thank you everyone. I made a boneheaded mistake every programming 101 class teaches. I did not rtrim the values. I don't know where the extra spaces came from since both values were retrieved from the database (different tables), but after rtrimming both values in the if statement, I finally got it to pass.