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}
%load_ext rpy2.ipython
Error importing in API mode: ImportError('/home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages/_rinterface_cffi_api.abi3.so: undefined symbol: R_ClosureEnv')
Trying to import in ABI mode.
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);
Error in callback <bound method StreamCapture.post_execute of <JupyROOT.helpers.utils.StreamCapture object at 0x7fd7309a8050>> (for post_execute), with arguments args (),kwargs {}:
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
File ~/miniforge3/envs/ROOT/lib/python3.14/site-packages/JupyROOT/helpers/utils.py:419, in StreamCapture.post_execute(self)
    416 self.ioHandler.EndCapture()
    418 # Print for the notebook
--> 419 out = self.ioHandler.GetStdout()
    420 err = self.ioHandler.GetStderr()
    421 if not transformers:

File ~/miniforge3/envs/ROOT/lib/python3.14/site-packages/JupyROOT/helpers/handlers.py:56, in IOHandler.GetStdout(self)
     55 def GetStdout(self):
---> 56    return _lib.JupyROOTExecutorHandler_GetStdout()

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 0: invalid start byte
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 [11]:
%%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 [ ]:
a=input("Value");
print(a)
[1] "4"

R¶

In [16]:
%%R
a <- readline(prompt = "Value")
print(a)
[1] "33"

Fortan¶

In [14]:
%%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 [15]:
main()
          42