WeixiangYan/CodeTransOcean huggingface dataset:
Task: Guess the number_With feedback







Python: import random

inclusive_range = (1, 100)

print("Guess my target number that is between %i and %i (inclusive).\n"
      % inclusive_range)
target = random.randint(*inclusive_range)
answer, i = None, 0
while answer != target:
    i += 1
    txt = input("Your guess(%i): " % i)
    try:
        answer = int(txt)
    except ValueError:
        print("  I don't understand your input of '%s' ?" % txt)
        continue
    if answer < inclusive_range[0] or answer > inclusive_range[1]:
        print("  Out of range!")
        continue
    if answer == target:
        print("  Ye-Haw!!")
        break
    if answer < target: print("  Too low.")
    if answer > target: print("  Too high.")

print("\nThanks for playing.")





Ada: with Ada.Numerics.Discrete_Random;
with Ada.Text_IO;
procedure Guess_Number_Feedback is
   function Get_Int (Prompt : in String) return Integer is
   begin
      loop
         Ada.Text_IO.Put (Prompt);
         declare
            Response : constant String := Ada.Text_IO.Get_Line;
         begin
            if Response /= "" then
               return Integer'Value (Response);
            end if;
         exception
            when others =>
               Ada.Text_IO.Put_Line ("Invalid response, not an integer!");
         end;
      end loop;
   end Get_Int;
   procedure Guess_Number (Lower_Limit : Integer; Upper_Limit : Integer) is
      subtype Number is Integer range Lower_Limit .. Upper_Limit;
      package Number_RNG is new Ada.Numerics.Discrete_Random (Number);
      Generator  : Number_RNG.Generator;
      My_Number  : Number;
      Your_Guess : Integer;
   begin
      Number_RNG.Reset (Generator);
      My_Number := Number_RNG.Random (Generator);
      Ada.Text_IO.Put_Line ("Guess my number!");
      loop
         Your_Guess := Get_Int ("Your guess: ");
         exit when Your_Guess = My_Number;
         if Your_Guess > My_Number then
            Ada.Text_IO.Put_Line ("Wrong, too high!");
         else
            Ada.Text_IO.Put_Line ("Wrong, too low!");
         end if;
      end loop;
      Ada.Text_IO.Put_Line ("Well guessed!");
   end Guess_Number;
   Lower_Limit : Integer;
   Upper_Limit : Integer;
begin
   loop
      Lower_Limit := Get_Int  ("Lower Limit: ");
      Upper_Limit := Get_Int  ("Upper Limit: ");
      exit when Lower_Limit < Upper_Limit;
      Ada.Text_IO.Put_Line ("Lower limit must be lower!");
   end loop;
   Guess_Number (Lower_Limit, Upper_Limit);
end Guess_Number_Feedback;
Task: Guess the number_With feedback








VB: Sub GuessTheNumberWithFeedback()
Dim Nbc&, Nbp&, m&, n&, c&

    Randomize Timer
    m = 11
    n = 100
    Nbc = Int((Rnd * (n - m + 1)) + m)
    Do
        c = c + 1
        Nbp = Application.InputBox("Choose a number between " & m & " and " & n & " : ", "Enter your guess", Type:=1)
        Select Case Nbp
            Case Is > Nbc: MsgBox "Higher than target!"
            Case Is < Nbc: MsgBox "Less than target!"
            Case Else: Exit Do
        End Select
    Loop
    MsgBox "Well guessed!" & vbCrLf & "You find : " & Nbc & " in " & c & " guesses!"
End Sub




Ada: with Ada.Numerics.Discrete_Random;
with Ada.Text_IO;
procedure Guess_Number_Feedback is
   function Get_Int (Prompt : in String) return Integer is
   begin
      loop
         Ada.Text_IO.Put (Prompt);
         declare
            Response : constant String := Ada.Text_IO.Get_Line;
         begin
            if Response /= "" then
               return Integer'Value (Response);
            end if;
         exception
            when others =>
               Ada.Text_IO.Put_Line ("Invalid response, not an integer!");
         end;
      end loop;
   end Get_Int;
   procedure Guess_Number (Lower_Limit : Integer; Upper_Limit : Integer) is
      subtype Number is Integer range Lower_Limit .. Upper_Limit;
      package Number_RNG is new Ada.Numerics.Discrete_Random (Number);
      Generator  : Number_RNG.Generator;
      My_Number  : Number;
      Your_Guess : Integer;
   begin
      Number_RNG.Reset (Generator);
      My_Number := Number_RNG.Random (Generator);
      Ada.Text_IO.Put_Line ("Guess my number!");
      loop
         Your_Guess := Get_Int ("Your guess: ");
         exit when Your_Guess = My_Number;
         if Your_Guess > My_Number then
            Ada.Text_IO.Put_Line ("Wrong, too high!");
         else
            Ada.Text_IO.Put_Line ("Wrong, too low!");
         end if;
      end loop;
      Ada.Text_IO.Put_Line ("Well guessed!");
   end Guess_Number;
   Lower_Limit : Integer;
   Upper_Limit : Integer;
begin
   loop
      Lower_Limit := Get_Int  ("Lower Limit: ");
      Upper_Limit := Get_Int  ("Upper Limit: ");
      exit when Lower_Limit < Upper_Limit;
      Ada.Text_IO.Put_Line ("Lower limit must be lower!");
   end loop;
   Guess_Number (Lower_Limit, Upper_Limit);
end Guess_Number_Feedback;
Task: Guess the number_With feedback

C: #include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define lower_limit 0
#define upper_limit 100

int main(){
  int number, guess;

  srand( time( 0 ) );
  number = lower_limit + rand() % (upper_limit - lower_limit + 1);

  printf( "Guess the number between %d and %d: ", lower_limit, upper_limit );

  while( scanf( "%d", &guess ) == 1 ){
    if( number == guess ){
      printf( "You guessed correctly!\n" );
      break;
    }
    printf( "Your guess was too %s.\nTry again: ", number < guess ? "high" : "low" );
  }

  return 0;
}
Task: Guess the number_With feedback



C#: using System;

class Program
{
    static void Main(string[] args)
    {
        const int from = 1;
        const int to = 10;

        int randomNumber = new Random().Next(from, to);
        int guessedNumber;

        Console.Write("The number is between {0} and {1}. ", from, to);
        while (true)
        {
            Console.Write("Make a guess: ");
            if (int.TryParse(Console.ReadLine(), out guessedNumber))
            {
                if (guessedNumber == randomNumber)
                {
                    Console.WriteLine("You guessed the right number!");
                    break;
                }
                else
                {
                    Console.WriteLine("Your guess was too {0}.", (guessedNumber > randomNumber) ? "high" : "low");
                }
            }
            else
            {
                Console.WriteLine("Input was not an integer.");
            }
        }

        Console.WriteLine();
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
Task: Guess the number_With feedback


C++: #include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
    std::srand(std::time(0));
    int lower, upper, guess;
    std::cout << "Enter lower limit: ";
    std::cin >> lower;
    std::cout << "Enter upper limit: ";
    std::cin >> upper;
    int random_number = lower + std::rand() % ((upper + 1) - lower);

    do
    {
        std::cout << "Guess what number I have: ";
        std::cin >> guess;
        if (guess > random_number)
            std::cout << "Your guess is too high\n";
        else if (guess < random_number)
            std::cout << "Your guess is too low\n";
        else
            std::cout << "You got it!\n";
    } while (guess != random_number);

    return 0;
}
Task: Guess the number_With feedback





Java: import java.util.Random;
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        Random random = new Random();
        long from = 1;
        long to = 100;
        int randomNumber = random.nextInt(to - from + 1) + from;
        int guessedNumber = 0;

        System.out.printf("The number is between %d and %d.\n", from, to);

        do
        {
            System.out.print("Guess what the number is: ");
            guessedNumber = scan.nextInt();
            if (guessedNumber > randomNumber)
                System.out.println("Your guess is too high!");
            else if (guessedNumber < randomNumber)
                System.out.println("Your guess is too low!");
            else
                System.out.println("You got it!");
        } while (guessedNumber != randomNumber);
    }
}
Task: Guess the number_With feedback







Python: import random

inclusive_range = (1, 100)

print("Guess my target number that is between %i and %i (inclusive).\n"
      % inclusive_range)
target = random.randint(*inclusive_range)
answer, i = None, 0
while answer != target:
    i += 1
    txt = input("Your guess(%i): " % i)
    try:
        answer = int(txt)
    except ValueError:
        print("  I don't understand your input of '%s' ?" % txt)
        continue
    if answer < inclusive_range[0] or answer > inclusive_range[1]:
        print("  Out of range!")
        continue
    if answer == target:
        print("  Ye-Haw!!")
        break
    if answer < target: print("  Too low.")
    if answer > target: print("  Too high.")

print("\nThanks for playing.")
Task: Guess the number_With feedback








VB: Public Sub Main() 
  
  Randomize 
  Dim guess As Integer, max As Integer = 20 
  Dim n As Integer = Int(Rnd * max) + 1 
  
  Print "Guess which number I
  
  Do 
    Print " Your guess : "
    Input guess 
    If guess > n And guess <= 20 Then 
      Print "Your guess is higher than the chosen number, try again" 
    Else If guess = n Then 
      Print "Correct, well guessed!" 
      Break
    Else If guess < n And guess >= 1 Then 
      Print "Your guess is lower than the chosen number, try again" 
    Else 
      Print "Your guess is inappropriate, try again"    
    End If 
  Loop

End
Task: Guess the number_With feedback




Go: package main

import (
    "fmt"
    "math/rand"
    "time"
)

const lower, upper = 1, 100

func main() {
    fmt.Printf("Guess integer number from %d to %d: ", lower, upper)
    rand.Seed(time.Now().Unix())
    n := rand.Intn(upper-lower+1) + lower
    for guess := n; ; {
        switch _, err := fmt.Scan(&guess); {
        case err != nil:
            fmt.Println("\n", err, "So, bye.")
            return
        case guess < n:
            fmt.Print("Too low. Try again: ")
        case guess > n:
            fmt.Print("Too high. Try again: ")
        default:
            fmt.Println("Well guessed!")
            return
        }
    }
}
Task: Guess the number_With feedback



C#: using System;

class Program
{
    static void Main(string[] args)
    {
        const int from = 1;
        const int to = 10;

        int randomNumber = new Random().Next(from, to);
        int guessedNumber;

        Console.Write("The number is between {0} and {1}. ", from, to);
        while (true)
        {
            Console.Write("Make a guess: ");
            if (int.TryParse(Console.ReadLine(), out guessedNumber))
            {
                if (guessedNumber == randomNumber)
                {
                    Console.WriteLine("You guessed the right number!");
                    break;
                }
                else
                {
                    Console.WriteLine("Your guess was too {0}.", (guessedNumber > randomNumber) ? "high" : "low");
                }
            }
            else
            {
                Console.WriteLine("Input was not an integer.");
            }
        }

        Console.WriteLine();
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}








Fortran: program Guess_a_number
  implicit none
  
  integer, parameter :: limit = 100
  integer :: guess, number
  real :: rnum
  
  write(*, "(a, i0, a)") "I have chosen a number between 1 and ", limit, &
                         " and you have to try to guess it." 
  write(*, "(a/)")  "I will score your guess by indicating whether it is higher, lower or the same as that number"
 
  call random_seed
  call random_number(rnum)
  number = rnum * limit + 1
  do
    write(*, "(a)", advance="no") "Enter quess: "
    read*, guess
    if(guess < number) then
      write(*, "(a/)") "That is lower"
    else if(guess > number) then
      write(*, "(a/)") "That is higher"
    else
      write(*, "(a)") "That is correct"
      exit
    end if
  end do
end program
Task: Guess the number_With feedback


C++: #include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
    std::srand(std::time(0));
    int lower, upper, guess;
    std::cout << "Enter lower limit: ";
    std::cin >> lower;
    std::cout << "Enter upper limit: ";
    std::cin >> upper;
    int random_number = lower + std::rand() % ((upper + 1) - lower);

    do
    {
        std::cout << "Guess what number I have: ";
        std::cin >> guess;
        if (guess > random_number)
            std::cout << "Your guess is too high\n";
        else if (guess < random_number)
            std::cout << "Your guess is too low\n";
        else
            std::cout << "You got it!\n";
    } while (guess != random_number);

    return 0;
}









Fortran: program Guess_a_number
  implicit none
  
  integer, parameter :: limit = 100
  integer :: guess, number
  real :: rnum
  
  write(*, "(a, i0, a)") "I have chosen a number between 1 and ", limit, &
                         " and you have to try to guess it." 
  write(*, "(a/)")  "I will score your guess by indicating whether it is higher, lower or the same as that number"
 
  call random_seed
  call random_number(rnum)
  number = rnum * limit + 1
  do
    write(*, "(a)", advance="no") "Enter quess: "
    read*, guess
    if(guess < number) then
      write(*, "(a/)") "That is lower"
    else if(guess > number) then
      write(*, "(a/)") "That is higher"
    else
      write(*, "(a)") "That is correct"
      exit
    end if
  end do
end program
Task: Guess the number_With feedback

C: #include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define lower_limit 0
#define upper_limit 100

int main(){
  int number, guess;

  srand( time( 0 ) );
  number = lower_limit + rand() % (upper_limit - lower_limit + 1);

  printf( "Guess the number between %d and %d: ", lower_limit, upper_limit );

  while( scanf( "%d", &guess ) == 1 ){
    if( number == guess ){
      printf( "You guessed correctly!\n" );
      break;
    }
    printf( "Your guess was too %s.\nTry again: ", number < guess ? "high" : "low" );
  }

  return 0;
}










Fortran: program Guess_a_number
  implicit none
  
  integer, parameter :: limit = 100
  integer :: guess, number
  real :: rnum
  
  write(*, "(a, i0, a)") "I have chosen a number between 1 and ", limit, &
                         " and you have to try to guess it." 
  write(*, "(a/)")  "I will score your guess by indicating whether it is higher, lower or the same as that number"
 
  call random_seed
  call random_number(rnum)
  number = rnum * limit + 1
  do
    write(*, "(a)", advance="no") "Enter quess: "
    read*, guess
    if(guess < number) then
      write(*, "(a/)") "That is lower"
    else if(guess > number) then
      write(*, "(a/)") "That is higher"
    else
      write(*, "(a)") "That is correct"
      exit
    end if
  end do
end program
Task: Guess the number_With feedback




Go: package main

import (
    "fmt"
    "math/rand"
    "time"
)

const lower, upper = 1, 100

func main() {
    fmt.Printf("Guess integer number from %d to %d: ", lower, upper)
    rand.Seed(time.Now().Unix())
    n := rand.Intn(upper-lower+1) + lower
    for guess := n; ; {
        switch _, err := fmt.Scan(&guess); {
        case err != nil:
            fmt.Println("\n", err, "So, bye.")
            return
        case guess < n:
            fmt.Print("Too low. Try again: ")
        case guess > n:
            fmt.Print("Too high. Try again: ")
        default:
            fmt.Println("Well guessed!")
            return
        }
    }
}







Fortran: program Guess_a_number
  implicit none
  
  integer, parameter :: limit = 100
  integer :: guess, number
  real :: rnum
  
  write(*, "(a, i0, a)") "I have chosen a number between 1 and ", limit, &
                         " and you have to try to guess it." 
  write(*, "(a/)")  "I will score your guess by indicating whether it is higher, lower or the same as that number"
 
  call random_seed
  call random_number(rnum)
  number = rnum * limit + 1
  do
    write(*, "(a)", advance="no") "Enter quess: "
    read*, guess
    if(guess < number) then
      write(*, "(a/)") "That is lower"
    else if(guess > number) then
      write(*, "(a/)") "That is higher"
    else
      write(*, "(a)") "That is correct"
      exit
    end if
  end do
end program
Task: Guess the number_With feedback





Java: import java.util.Random;
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        Random random = new Random();
        long from = 1;
        long to = 100;
        int randomNumber = random.nextInt(to - from + 1) + from;
        int guessedNumber = 0;

        System.out.printf("The number is between %d and %d.\n", from, to);

        do
        {
            System.out.print("Guess what the number is: ");
            guessedNumber = scan.nextInt();
            if (guessedNumber > randomNumber)
                System.out.println("Your guess is too high!");
            else if (guessedNumber < randomNumber)
                System.out.println("Your guess is too low!");
            else
                System.out.println("You got it!");
        } while (guessedNumber != randomNumber);
    }
}






Fortran: program Guess_a_number
  implicit none
  
  integer, parameter :: limit = 100
  integer :: guess, number
  real :: rnum
  
  write(*, "(a, i0, a)") "I have chosen a number between 1 and ", limit, &
                         " and you have to try to guess it." 
  write(*, "(a/)")  "I will score your guess by indicating whether it is higher, lower or the same as that number"
 
  call random_seed
  call random_number(rnum)
  number = rnum * limit + 1
  do
    write(*, "(a)", advance="no") "Enter quess: "
    read*, guess
    if(guess < number) then
      write(*, "(a/)") "That is lower"
    else if(guess > number) then
      write(*, "(a/)") "That is higher"
    else
      write(*, "(a)") "That is correct"
      exit
    end if
  end do
end program
Task: Guess the number_With feedback







Python: import random

inclusive_range = (1, 100)

print("Guess my target number that is between %i and %i (inclusive).\n"
      % inclusive_range)
target = random.randint(*inclusive_range)
answer, i = None, 0
while answer != target:
    i += 1
    txt = input("Your guess(%i): " % i)
    try:
        answer = int(txt)
    except ValueError:
        print("  I don't understand your input of '%s' ?" % txt)
        continue
    if answer < inclusive_range[0] or answer > inclusive_range[1]:
        print("  Out of range!")
        continue
    if answer == target:
        print("  Ye-Haw!!")
        break
    if answer < target: print("  Too low.")
    if answer > target: print("  Too high.")

print("\nThanks for playing.")




Fortran: program Guess_a_number
  implicit none
  
  integer, parameter :: limit = 100
  integer :: guess, number
  real :: rnum
  
  write(*, "(a, i0, a)") "I have chosen a number between 1 and ", limit, &
                         " and you have to try to guess it." 
  write(*, "(a/)")  "I will score your guess by indicating whether it is higher, lower or the same as that number"
 
  call random_seed
  call random_number(rnum)
  number = rnum * limit + 1
  do
    write(*, "(a)", advance="no") "Enter quess: "
    read*, guess
    if(guess < number) then
      write(*, "(a/)") "That is lower"
    else if(guess > number) then
      write(*, "(a/)") "That is higher"
    else
      write(*, "(a)") "That is correct"
      exit
    end if
  end do
end program
Task: Guess the number_With feedback






PHP: <?php

session_start();

if(isset($_SESSION['number']))
{
   $number = $_SESSION['number'];
}
else
{
   $_SESSION['number'] = rand(1,10);
}


if($_POST["guess"]){
    $guess  = htmlspecialchars($_POST['guess']);

	echo $guess . "<br />";
    if ($guess < $number)
	{ 
        echo "Your guess is too low";
    }
	elseif($guess > $number)
	{
        echo "Your guess is too high";
    }
	elseif($guess == $number)
	{
        echo "You got the correct number!";
    }
    
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<title>Guess A Number</title>
	</head>

	<body>
		<form action="<?=$_SERVER['PHP_SELF'] ?>" method="post" name="guess-a-number">
		    <label for="guess">Guess A Number:</label><br/ >
		    <input type="text" name="guess" />
		    <input name="number" type="hidden" value="<?= $number ?>" />
		    <input name="submit" type="submit" />
		</form>
	</body>
</html>





Fortran: program Guess_a_number
  implicit none
  
  integer, parameter :: limit = 100
  integer :: guess, number
  real :: rnum
  
  write(*, "(a, i0, a)") "I have chosen a number between 1 and ", limit, &
                         " and you have to try to guess it." 
  write(*, "(a/)")  "I will score your guess by indicating whether it is higher, lower or the same as that number"
 
  call random_seed
  call random_number(rnum)
  number = rnum * limit + 1
  do
    write(*, "(a)", advance="no") "Enter quess: "
    read*, guess
    if(guess < number) then
      write(*, "(a/)") "That is lower"
    else if(guess > number) then
      write(*, "(a/)") "That is higher"
    else
      write(*, "(a)") "That is correct"
      exit
    end if
  end do
end program
Task: Guess the number_With feedback

C: #include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define lower_limit 0
#define upper_limit 100

int main(){
  int number, guess;

  srand( time( 0 ) );
  number = lower_limit + rand() % (upper_limit - lower_limit + 1);

  printf( "Guess the number between %d and %d: ", lower_limit, upper_limit );

  while( scanf( "%d", &guess ) == 1 ){
    if( number == guess ){
      printf( "You guessed correctly!\n" );
      break;
    }
    printf( "Your guess was too %s.\nTry again: ", number < guess ? "high" : "low" );
  }

  return 0;
}









COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Guess-With-Feedback.

       DATA DIVISION.
       LOCAL-STORAGE SECTION.
       01  Seed       PIC 9(8).
       01  Random-Num PIC 99.
       01  Guess      PIC 99.
 
       PROCEDURE DIVISION.
           ACCEPT Seed FROM TIME
           COMPUTE Random-Num =
               FUNCTION REM(FUNCTION RANDOM(Seed) * 1000, 10) + 1

           DISPLAY "Guess a number between 1 and 10:"
 
           PERFORM FOREVER
               ACCEPT Guess
 
               IF Guess > Random-Num
                   DISPLAY "Your guess was too high."
               ELSE IF Guess < Random-Num
                   DISPLAY "Your guess was too low."
               ELSE
                   DISPLAY "Well guessed!"
                   EXIT PERFORM
           END-PERFORM
 
           GOBACK
           .