Loop and Jump - Keyboard Input¶

you can watch parallel the video for C/C++ (german): https://www.youtube.com/watch?v=aob0SwTaF2Q&list=PLEWVM-KBUSpmWSfyoFdD_hLWAY_9tTgi5&index=17 https://www.youtube.com/watch?v=gjRcvn9TM-o&list=PLEWVM-KBUSpmWSfyoFdD_hLWAY_9tTgi5&index=18

In [1]:
import ROOT
In [2]:
import fortranmagic
%load_ext fortranmagic
import os
import sys

import numpy as np

if sys.platform.startswith("win"):
        # Depends of system, python builds, and compilers compatibility.
        # See below.
    f_config = "--fcompiler=gnu95 --compiler=mingw32"
else:
        # For Unix, compilers are usually more compatible.
    f_config = ""

    # Disable only deprecated NumPy API warning without disable any APIs.
f_config += " --extra '-DNPY_NO_DEPRECATED_API=0'"

%fortran_config {f_config}
New default arguments for %fortran:
	 --extra '-DNPY_NO_DEPRECATED_API=0'
In [3]:
%jsroot on

C++¶

In [4]:
%%cpp
#include <iostream>
using namespace std;
int a=0;
//read in a number
//cin >>a;  //only works in compiled program
cout <<"a="<<a<<endl;
a=0
In [5]:
%%cpp
#include <iostream>
#include <string>
using namespace std;
string s;
//read in a whole line
//getline(cin,s);  //only works in compiled program
cout <<"a="<<a<<endl;
a=0

C¶

In [6]:
%%cpp
#include <stdio.h>
int a;
//scanf("%d",&a); //only works in compiled code
printf("%d",a);
0
In [7]:
%%cpp
#include <stdio.h>
double d;
//scanf("%lf",&d); //only works in compiled code
printf("%lf",d);
0.000000
In [8]:
%%cpp
#include <stdio.h>
char c;

//scanf("%c",&d); //only works in compiled code
printf("%c",d);
input_line_63:6:13: warning: format specifies type 'int' but the argument has type 'double' [-Wformat]
printf("%c",d);
        ~~  ^
        %f
In [9]:
%%cpp
#include <stdio.h>
char s[1000];
//single word
//scanf("%999s",&s); //only works in compiled code
printf("%s",s);
In [10]:
%%cpp
#include <stdio.h>
char s[1000];
//reads till return
//scanf("%999[^\n]s",s); //only works in compiled code
printf("%s",s);

Javascript¶

In [17]:
%%js //the next line is only necessary in jupyter notebooks
element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}

let a=0;
a=prompt("Value","0"); //only works outside of jupyter notebook in browser

Python¶

In [12]:
a=input("Value");
print(a)
9

Fortan¶

In [13]:
%%fortran 

! program and subroutine exchanged due to jupyternotebook

! program main
subroutine main()
    implicit none

    integer :: a
    !read (*,*) a ! only works in compiled program
    a=42
    print *,a
! end program
end subroutine main
In [14]:
main()
          42