lundi 1 octobre 2018

OCaml - If then else

I want to do several things inside a if then else. I read a string and then I check it's lenght. If it's 3 it does X, if it's 4 it does Y.

I tried:

let str = read_line ()

let first_approach () =
    if String.lenght str = 3
    then let    char1 = String.get str 0
         let    char2 = String.get str 1
         let    char3 = String.get str 2
    else ()

let second_approach () =
    if String.lenght str = 3
    then let    char1 = String.get str 0
         let    char2 = String.get str 1
         let    char3 = String.get str 2
         let    char4 = String.get str 3
    else ()

I want to know how to make all things inside then work. Because I get Syntax error. Thank you in advance.

How to use a string in an if statement? [duplicate]

This question already has an answer here:

I need to make it so that less than 3 characters results in an error message, and 3 or more prints the statement. So for the beginning of the if statement I put if(refNumber > " ") and so on, however, the string comes up as an error.

Bringing data from other workbooks - error in nested if

I have this code in which I am bringing data from several workbooks into one. The data of each workbook needs to be added into a specific range depending on the source. To do this I nested some IFs with the partial name of the file as condition and giving the action of sending the values to the desired range, but when I run the code it only opens all the workbooks without performing any action. I already did some research and did not find anything to help me with my problem

Sub Update_Database()

Dim directory As String
Dim fileName As String

Application.ScreenUpdating = False

With Application.FileDialog(msoFileDialogFolderPicker)
    .AllowMultiSelect = False
    .Show
    directory = .SelectedItems(1)
    Err.Clear
End With

fileName = Dir(directory & "\", vbReadOnly)

Dim mwb As Workbook
Set mwb = Workbooks("OEE_Database_Final.xlsm")

Do While fileName <> ""
    On Error GoTo ProcExit
    With Workbooks.Open(fileName:=directory & "\" & fileName, UpdateLinks:=False, ReadOnly:=True)
        If (fileName = "NOM*.xlsx") Then
            ActiveWorkbook.Sheets("Database").Range("O9:Z290").Value = mwb.Sheets("Database").Range("O9:Z290")
            ActiveWorkbook.Close SaveChanges:=False
        ElseIf (fileName = "SZE*.xlsx") Then
            ActiveWorkbook.Sheets("Database").Range("O291:Z537").Value = mwb.Sheets("Database").Range("O291:Z537")
            ActiveWorkbook.Close SaveChanges:=False
        ElseIf (fileName = "VEC*.xlsx") Then
            ActiveWorkbook.Sheets("Database").Range("O538:Z600").Value = mwb.Sheets("Database").Range("O538:Z600")
            ActiveWorkbook.Close SaveChanges:=False
        ElseIf (fileName = "KAY*.xlsx") Then
            ActiveWorkbook.Sheets("Database").Range("O601:Z809").Value = mwb.Sheets("Database").Range("O601:Z809")
            ActiveWorkbook.Close SaveChanges:=False
        ElseIf (fileName = "BBL*.xlsx") Then
            ActiveWorkbook.Sheets("Database").Range("O810:Z952").Value = mwb.Sheets("Database").Range("O810:Z952")
            ActiveWorkbook.Close SaveChanges:=False
        ElseIf (fileName = "POG*.xlsx") Then
            ActiveWorkbook.Sheets("Database").Range("O953:Z1037").Value = mwb.Sheets("Database").Range("O953:Z1037")
            ActiveWorkbook.Close SaveChanges:=False
        ElseIf (fileName = "SC1*.xlsx") Then
            ActiveWorkbook.Sheets("Database").Range("O1038:Z1159").Value = mwb.Sheets("Database").Range("O1038:Z1159")
            ActiveWorkbook.Close SaveChanges:=False
        ElseIf (fileName = "SC2*.xlsx") Then
            ActiveWorkbook.Sheets("Database").Range("O1160:Z1200").Value = mwb.Sheets("Database").Range("O1160:Z1200")
            ActiveWorkbook.Close SaveChanges:=False
        ElseIf (fileName = "SLP*.xlsx") Then
            ActiveWorkbook.Sheets("Database").Range("O1201:Z1263").Value = mwb.Sheets("Database").Range("O1201:Z1263")
            ActiveWorkbook.Close SaveChanges:=False
        ElseIf (fileName = "UIT*.xlsx") Then
            ActiveWorkbook.Sheets("Database").Range("O1264:Z1348").Value = mwb.Sheets("Database").Range("O1264:Z1348")
            ActiveWorkbook.Close SaveChanges:=False
        ElseIf (fileName = "ANE*.xlsx") Then
            ActiveWorkbook.Sheets("Database").Range("O1349:Z1823").Value = mwb.Sheets("Database").Range("O1349:Z1823")
            ActiveWorkbook.Close SaveChanges:=False
        ElseIf (fileName = "HAL*.xlsx") Then
            ActiveWorkbook.Sheets("Database").Range("O1824:Z2077").Value = mwb.Sheets("Database").Range("O1824:Z2077")
            ActiveWorkbook.Close SaveChanges:=False
        ElseIf (fileName = "SHX*.xlsx") Then
            ActiveWorkbook.Sheets("Database").Range("O2078:Z2242").Value = mwb.Sheets("Database").Range("O2078:Z2242")
            ActiveWorkbook.Close SaveChanges:=False
        ElseIf (fileName = "BAY*.xlsx") Then
            ActiveWorkbook.Sheets("Database").Range("O2243:Z2415").Value = mwb.Sheets("Database").Range("O2243:Z2415")
            ActiveWorkbook.Close SaveChanges:=False
        ElseIf (fileName = "TAM*.xlsx") Then
            ActiveWorkbook.Sheets("Database").Range("O2416:Z2522").Value = mwb.Sheets("Database").Range("O2416:Z2522")
            ActiveWorkbook.Close SaveChanges:=False
        ElseIf (fileName = "PUC*.xlsx") Then
            ActiveWorkbook.Sheets("Database").Range("O2523:Z2607").Value = mwb.Sheets("Database").Range("O2523:Z2607")
            ActiveWorkbook.Close SaveChanges:=False
        ElseIf (fileName = "JOF*.xlsx") Then
            ActiveWorkbook.Sheets("Database").Range("O2608:Z2648").Value = mwb.Sheets("Database").Range("O2608:Z2648")
            ActiveWorkbook.Close SaveChanges:=False
        ElseIf (fileName = "MAV*.xlsx") Then
            ActiveWorkbook.Sheets("Database").Range("O2649:Z2945").Value = mwb.Sheets("Database").Range("O2649:Z2945")
            ActiveWorkbook.Close SaveChanges:=False
        End If
    End With
    fileName = Dir
Loop

Application.ScreenUpdating = True


ProcExit:
Exit Sub

End Sub

Multiple if statements without an else C++

The code I have to write is basically a mini bank. It asks for an initial amount, the type of action and a second operator for that action. I'm not allowed to you else but am allowed to if statements (i don't understand why) nor am I allowed to use a loop or an array.

    #include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int operand1;
int operand2;
float output;
char action;



int main()
{
   cout << fixed << showpoint << setprecision(2);

   cout << "Enter the initial balance [1-1000]: " << endl;
   cin >> operand1;

   cout << "Enter an action (D, W, I or C):" << endl;
   cin >> action;

   cout << "Enter the second operand:" << endl;
   cin >> operand2;


   if ((action != 'D' && action != 'W' && action != 'I' && action != 'C') || (operand1 > 1000 || operand1 < 1) || 
       (action == 'I' && operand2 > 15 || operand2 < 1) || (action == 'C' && operand2 != 20 && operand2 != 10 && operand2 != 5) ||
       (operand2 > 1000 || operand2 < 1))
   {
       cout << "Input out of range" << endl;
       return 0;
   }

   if (action == 'D')
   {
      output = (operand1 + operand2);
      cout << "The new account balance is " << output << endl;
   }

   if (action == 'W')
   {
      output = (operand1 - operand2);
      if (output<0)
      {
          cout << "Input out of range" << endl;
          return 0;
      }
       cout << "The new account balance is " << output << endl;
   }


   if (action == 'I')
   {

       output = ((float)operand1 + (((float)operand2 / 100) * (float)operand1));
       cout << "The new account balance is " << output << endl;
   }

   if (action == 'C')
   {
       output = operand1 % operand2;
       cout << operand1 / operand2 << " bills dispensed plus " << output << endl;
   }

    cin.get();
    cin.get();
    return 0;
}

This is the code so far. on certain instances i get multiple erros instead of just one. for example if input i should get: Enter the initial balance [1-1000]: 1030

Enter an action (D, W, I or C): D

Enter the second operand: 40

Input out of range

Input out of range, however, it seems to just move on when it sees the error anyway and I get the output:

Enter the initial balance [1-1000]:

1030

Input out of range

Enter an action (D, W, I or C):

D

Enter the second operand:

40

The new account balance is 1070.00

I can't seem to figure out to have only one output and for it to just display the error with no balance without using an else statement.

If else function for R

My dataframe is as follow:

'data.frame':   7 obs. of  3 variables:
 $ Currency : Factor w/ 3 levels "EUR","GBP","USD": 3 3 1 2 3 1 2
 $ Amount.LC: int  100 500 400 200 350 100 500
 $ FX.Rate  : Factor w/ 3 levels "0,89","1,201",..: 2 2 3 1 2 3 1

Based on an "if-else" function I tried to calculate a new column, called Amount.GC, depending whether a "EUR" is in column Currency or not. If "EUR" than I want to have the same value as in column Amount.LC. Else I want to have the division of Amount.LC and FX.Rate (Amount.LC/FX.Rate).

My code is as follow:

for (i in df$Currency){if (i == "EUR"){df$Amount.GC <- df$Amount.LC}
else{df$Amount.GC <- c(df$Amount.LC)/c(df$FX.Rate)}}

Unfortunately it doesn't work. Could someone please help me?

Python: Finding numbers in list

Code How can I make the number to see if it is int he list range? I'm confused what is wrong with the code.

Having a syntax error with elif statement

i'm a python newbie

in order to learn, i wantto create a euro-dollar converter. to me the code seems ok but it return invalid syntax ? enter image description here