Input the math expression such as 3+2*{1+2*[-4/(8-6)+7]} make the python program calculate. The problem is, it gives no output. How to fix this problem.
while True:
try:
express = input()
express.replace("[", "(")
express.replace("{", "(")
express.replace("]", ")")
express.replace("}", ")")
print(eval(express))
except:
break
Update:
After each replacement, we must update the expression, otherwise the change will only reflect to the given expression & after each replace statement it will change and only modify the given expression
while True:
try:
express = input()
x = express.replace("[", "(")
y = x.replace("{", "(")
z = y.replace("]", ")")
w = z.replace("}", ")")
print(eval(w))
except:
break
Related
In an Access Form I can enter values in a TextBox called LenD. Sometimes I need to check the input code in order to split standard text from a code. For instance:
noumnoum[codecode]
To obtain:
noumnoum
So that, I use this:
If InStr(1, Me!LenD, "[") Then
Me!LenD = Left(Mid(Me!LenD, InStr(1, Me!LenD, "[") + 1, (InStr(1, Me!LenteD, "]")) - (InStr(1, Me!LenD, "[")) - 1), 50)
Else
Me!LenD = Left(Me!LenD, 50)
End If
But I just obtain the string inside the "[" "]". My aim would be to obtain the string that is on the left of the original String. Any idea on why it does not work?
You can use Split and simplify this:
If Not IsNull(Me!LenD) Then
Me!LenD = Left(Split(Me!LenD, "[")(0), 50)
End If
Dim x as Long
x = Instr(me!LenD,"[")
If x > 0 then
Me!LenD = Left(Me!lenD,x-1)
Else
me!LenD = Left(Me!LenD,50)
End If
I've gotten stuck with this VBA code. Any help would be greatly appreciated. I'm trying to change the first letters of 2 words from lower case to upper case. Also, how should I take the space in between these two words into consideration in the code?
I haven't been able to execute the code as I keep getting this compile error: "Argument not optional".
Function Properword(Text)
Dim rText
rText = Len(rText)
If rText(Mid(1, 1)) = LCase(Str) Then
rText = UCase(Str)
If rText(Mid(6, 1)) = LCase(Str) Then
rText = UCase
End If
End Function
Cheers!
First of all, you don't have to use UDF. Simply use inbuilt WorksheetFunction.Proper function to achieve ProperCase.
If you still want to create UDF, an example would be
Function Properword(strTxt)
Dim arrTxt
arrTxt = Split(strTxt, " ")
For i = LBound(arrTxt) To UBound(arrTxt)
arrTxt(i) = UCase(Left(arrTxt(i), 1)) & Mid(arrTxt(i), 2)
Next
Properword = Join(arrTxt, " ")
End Function
Finally, issues with your code
rText = Len(rText) ~~ this means rText will contain a numeric value because Len returns the lenght of the string
If rText(Mid(1, 1)) = LCase(Str) Then ~~ Mid takes the string as first argument followed by start point and then end point (optional).
not sure what you were trying to do in the following lines.
rText = UCase(Str)
If rText(Mid(6, 1)) = LCase(Str) Then
rText = UCase
In addition to the Excel function PROPER
str = WorksheetFunction.Proper("UPPER lower") ' "Upper Lower"
There is also the VBA.StrConv function:
str = StrConv("UPPER lower", vbProperCase) ' "Upper Lower"
To convert only parts of the string to uppercase, you can use RegEx, or the Mid statement:
Mid(str, 1, 1) = UCase(Mid(str, 1, 1)) ' makes the first letter uppercase
I've had to move a whole directory structure (thousands of directories and files) from the Mac to the PC, but now on the PC for some reason some of the folder names have a character on the end of value binary zero ( a few have this in the middle). I need to clean this up since it crashes a macro which tries to read these directories.
I've tried the following code in a vba-word macro using the Replace function (as part of a larger program that walks through the directory tree) but the Replace function doesn't seem to catch chr(0) .
Set current_folder = fso.GetFolder(source_folder.Path)
current_folder_name = current_folder.Name
cleaned_folder_name = Replace(current_folder_name, Chr(0), " ")
Selection.TypeText "Old directory name: " & current_folder_name
Selection.TypeParagraph
Selection.TypeText "New directory name: " & cleaned_folder_name
Selection.TypeParagraph
If current_folder_name <> cleaned_folder_name Then
current_folder.Name = cleaned_folder_name
End If
I've also tried:
cleaned_folder_name = Replace(current_folder_name, Chr(0), " ", 1, -1, vbBinaryCompare)
How I can get the Replace function to replace binary 0 in a string by a blank.
Or does anyone knows a different approach to cleaning up these directory and file names that would work.
Thanks,
Harry
This should do it:
Dim OldFolderName As String, NewFolderName As String
OldFolderName = source_folder.Path
If InStr(OldFolderName, Chr(0)) > 0 Then
'NewFolderName = Application.Clean(OldFolderName) 'Works on some versions
NewFolderName = Application.WorksheetFunction.Clean(OldFolderName) 'By Gene Skuratovsky
Name OldFolderName As NewFolderName
End If
Edit2: Probably best to use the Clean() method.
In my case Replace() and InStr() don't work with Chr(0) (can't see it not displaying any error), but this removal can be done for example this way:
If Mid$(str, 1, 1) = Chr$(0) Then str = Mid$(str, 2)
If Mid$(str, Len(str), 1) = Chr$(0) Then str = Left$(str, Len(str) - 1)
This is Null removal from ends, for example from objFile.ExtendedProperty("Dimensions") value, like "?600 x 400?". Nulls (?) are insterted here in Windows 10 but not in Windows XP.
You can write a custom loop function to determine what character you are having issues with:
OldFolderName = source_folder.Path
For x = 0 To 255
If InStr(OldFolderName, Chr(x)) > 0 Then
MsgBox "Chr(" & x & ") is found at position " & InStr(OldFolderName, Chr(x))
EndIf
Next x
The following function was given to me via an answer that I asked earlier today.
What I'm trying to do is to remove a character from a string in Excel using VBA. However, whenever the function runs, it ends up erasing the value stored and returning a #!VALUE error. I cannot seem to figure out what is going on. Anyone mind explaining an alternative:
Function ReplaceAccentedCharacters(S As String) As String
Dim I As Long
With WorksheetFunction
For I = 1 To Len(S)
Select Case Asc(Mid(S, I, 1))
' Extraneous coding removed. Leaving the examples which
' do work and the one that is causing the problem.
Case 32
S = .Replace(S, I, 1, "-")
Case 94
S = .Replace(S, I, 1, "/")
' This is the coding that is generating the error.
Case 34
S = .Replace(S, I, 1, "")
End Select
Next I
End With
ReplaceAccentedCharacters = S
End Function
When the string contains a " (or character code 34 in Decimal, 22 in Hexadecimal... I used both) it is supposed to remove the quotation mark. However, instead, Excel ignores it, and still returns the " mark anyway.
I then tried to go ahead and replace the .Replace() clause with another value.
Case 34
S = .Replace(S, I, 1, "/")
End Select
Using the code above, the script indeed does replace the " with a /.
I ended up finding the following example here in Stack Overflow:
https://stackoverflow.com/a/7386565/692250
And in the answer given, I see the same exact code example similar to the one that I gave and nothing. Excel is still ignoring the quotation mark. I even went so far as to expand the definition with curly braces and still did not get anything.
Try this:
Function blah(S As String) As String
Dim arr, i
'array of [replace, with], [replace, with], etc
arr = Array(Chr(32), "-", Chr(94), "/", Chr(34), "")
For i = LBound(arr) To UBound(arr) Step 2
S = Replace(S, arr(i), arr(i + 1))
Next i
blah = S
End Function
This function was designed to replace one character with another. It was not designed to replace a character with nothing. What happens when you try to replace a character with nothing is that the Counter for iterating through the word will now look (at the last iteration) for a character position that is greater than the length of the word. That returns nothing, and when you try to determine ASC(<nothing>) an error occurs. Other errors in the replacement routine will also occur when the length of the string is changed while the code is running
To modify the routine to replace a character with nothing, I would suggest the following:
In the Case statements:
Case 34
S = .Replace(S, I, 1, Chr(1))
And in the assignment statement:
ReplaceAccentedCharacters = Replace(S, Chr(1), "")
Note that VBA Replace is different from Worksheetfunction Replace
Are there any tools available for automatically formatting vb.net code - specifically for adding line breaks at a predefined line length? I'm working with a lot of code with long lines (thousands of lines), and manually reformatting it is quite time consuming. I've seen a number of tools for rearranging code into regions etc., but haven't found any that reformat with line breaks. Free would be great.
Try having VS auto-wrap your lines. The option should be in the Tools | Options | Basic | Settings | Word Wrap.
Another thing to do is go to the Edit | Advanced | Format Document menu option, which helps clear the air with not well-formed documents.
A 3rd option is to install DevExpress' Code Rush Xpress add-on, which add's very handy vertical lines for when code blocks begin and end, and also helps in refactoring code. You can get it from here: http://devexpress.com/Products/Visual_Studio_Add-in/CodeRushX/. It's free, but doesn't support the Express editions of Visual Studio.
Use Visual Studio 2008 you have to use Ctrl + A + K + F for formatting your c#, vb code
I know this was posted a long time ago. But if ever someone had the same problem, try this sub I made. The sub will have two outputs (Textbox1 = Code with breaks, Textbox3 = a one liner code).
Create two textboxes (named Textbox1 and Textbox3) and a button (Button1)
Create a sub (name what you want) and enter this code:
Try
Dim x As String = TextBox1.Text
x = x.Replace("& """, "")
x = x.Replace(""" _", "")
x = x.Replace("""", "")
x = x.Replace(vbNewLine, "")
x = x.Replace(vbTab, "")
While x.Contains(" ") '2 spaces.
x = x.Replace(" ", " ") 'Replace with 1 space.
End While
TextBox3.Text = x
Dim l As Integer = Len(x)
Dim xlim As Integer = InputBox("Specify the maximum number of characters for each line:", "Line Max", 66)
Dim ylim As Double = 0
TextBox1.Text = ""
ylim = l / xlim
If Int(ylim) <> ylim Then
ylim = Int(ylim) + 1
Else
ylim = Int(ylim)
End If
Dim una As String = "", huli As String = ""
Dim mynewstring As String = ""
Dim startin As Integer = 1
For i = 1 To ylim
If i = 1 Then
una = """"
Else
una = vbTab & "& """
End If
If i = ylim Then
huli = """"
Else
huli = """ _"
End If
mynewstring = mynewstring & una & Strings.Mid(x, startin, xlim) & huli & vbNewLine
startin += xlim
Next
TextBox1.Text = mynewstring
Catch ex As Exception
MsgBox(ex.Message)
End Try
P.S. I did not add the code to restore your original input.