dimanche 1 novembre 2020

Logical && operator in php

Hello everyone I started to study php but there a few things about logical operators that not convincing me.

<?php
    $a = "hello";
    $b = 3;

    if ($a && $b === 3) {
        echo "true";
    } else {
        echo "false";
    }
?>

Why if I try to check triple equality the if statement print true? $a it's a string and $b a number, and then also if I put in $a a number the result it's the same

A simple if-else game in java [closed]

I am a student, I have been writing this code in java to simulate a type of hand cricket game in java. The code is working but instead of batting once and then bowling it is asking me to do any one of the previous twice before giving the result.

I have tried all I could have. Is there any thing that I can do which is pretty simple and of my understanding . Or do I need to learn some more things before proceeding to continue with this project.

   import java.util.*;

    public class Main_Game {
    public static String input() {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter your name: ");
    String name=sc.nextLine();
    return name;
}
public static void display(String name) {
    System.out.println("\n\nHello "+name);
    System.out.println("Welcome to the game !");
    System.out.println("The rules of the game are very simple! \nyou enter a number less than 7 and computer enters another number.\nIf those numbers are same its an out depending on your choice of playing\n\n");
    
}
public static boolean toss() {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter what you choose Heads or Tails ");
    String ch_toss=sc.nextLine();
    int n;
    if (ch_toss=="Tails") {
        n=1;
    } else {
        n=0;
    }
    int rand=(int)(Math.random()*2)+0,flag=0;
    if (n==rand) {
        return true;
    } else {
        return false;
    }
    
}
public static int batting() {
    Scanner sc=new Scanner(System.in);
    int wickets=0,runs=0;
    while(wickets<4){
        int bat=(int)(Math.random()*6)+1;
        System.out.print("Enter your hit: ");
        int bowl=sc.nextInt();
        System.out.println("The computers throw: "+bat);
        if(bowl<7){
            if(bowl==bat){
                System.out.println("Out !!!");
                wickets++;
            }
            else{
                runs+=bat;
            }
        }
    }
    return runs;
    
}
public static int bowling() {
    Scanner sc=new Scanner(System.in);
    int wickets=0,runs=0;
    while(wickets<4){
        int bowl=(int)(Math.random()*6)+1;
        System.out.print("Enter your throw: ");
        int bat=sc.nextInt();
        System.out.println("The computers ball: "+bowl);
        if(bat<7){
            if(bowl==bat){
                System.out.println("Out !!!");
                wickets++;
            }
            else{
                runs+=bat;
            }
        }
    }
    return runs;
    
}

public static int batting2() {
    Scanner sc=new Scanner(System.in);
    int wickets2=0,runs2=0;
    while(wickets2<4){
        int bat=(int)(Math.random()*6)+1;
        System.out.print("Enter your hit: ");
        int bowl=sc.nextInt();
        System.out.println("The computers throw: "+bat);
        if(bowl<7){
            if(bowl==bat){
                System.out.println("Out !!!");
                wickets2++;
            }
            else{
                runs2+=bat;
            }
        }
    }
    return runs2;
    
}
public static int bowling2() {
    Scanner sc=new Scanner(System.in);
    int wickets2=0,runs2=0;
    while(wickets2<4){
        int bowl=(int)(Math.random()*6)+1;
        System.out.print("Enter your throw: ");
        int bat=sc.nextInt();
        System.out.println("The computers hit: "+bowl);
        if(bat<7){
            if(bowl==bat){
                System.out.println("Out !!!");
                wickets2++;
            }
            else{
                runs2+=bat;
            }
        }
    }
    return runs2;
    
}



public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    String won_toss_ch="";
    String comp_ch="";
    String won_toss_ch2="";
    display(input());
    int my_ch=0;
    boolean res_toss=toss();
    if(res_toss) {
        System.out.println("You won the toss :)");
        my_ch=1;
    }
    else {
        my_ch=0;
        int ch_comp=(int)(Math.random()*2)+0;
        if (ch_comp==1) {
            comp_ch="Batting";
        } 
        else {
            comp_ch="Bowling";
        }
        System.out.println("You lost the toss :( The computer chose "+comp_ch);
    }
    if (my_ch==1) {
        System.out.println("What do you choose Bat or Ball ?");
        won_toss_ch=sc.nextLine();
    } 
    else {
        if (comp_ch=="Batting") {
            System.out.println("You are bowling");
            won_toss_ch="Bowling";
            won_toss_ch2="Batting";
        } 
        else {
            System.out.println("You are batting");
            won_toss_ch="Batting";
            won_toss_ch2="Bowling";
        }
    }
    if (won_toss_ch=="Batting") {
        System.out.println("\n\nYou made "+batting()+" runs");
    } 
    else {
        System.out.println("\n\nComputer made "+bowling()+" runs");
    }
    
    if (won_toss_ch2=="Batting") {
        System.out.println("\n\nYou are Batting now");
        System.out.println("\n\nYou made "+batting2()+" runs");
    
    } 
    else if (won_toss_ch2=="Bowling") {
        System.out.println("\n\nYou are Bowling now");
        System.out.println("\n\nComputer made "+bowling2()+" runs");
    }
    if (batting2()>batting()) {
        System.out.println("\n\nYou won");
        
    } 
    else {
        System.out.println("\n\nComputer won");
    }

}

}

Test if a string represents a number

Building a simple calculator for my first Python project and encountered a question shown in the comments below:

special_keys = ["`", "~", "!", "@", " ", "#", "$"]

while True:
    num1 = input("Enter First Number: ")
    if num1.isalpha():
        print("Invalid, Try Again.")
        continue

     # elif num1 contains an element or part of an
     # element in special_keys do the following:
         # print("Invalid, Try Again.")
         # continue

    else:
    num1 = float(num1)
    break

C# Console Application that Outputs a category based on input

I have a task to develop a windows application that collects employee information: Name and Number of items sold as input and outputs a summary of information in a way that groups the Salesperson based on the number of items sold (below 50 items, 50-99 items, 100-199 items and a level above 199 items) See below. Can you help me with this code. Here is the code am working with thus far:

using System;

namespace L0002B
{
    public class Salesperson
    {
        string Name { get; set; }
        int Quantity_Sold { get; set; }
        static void Main(string[] args)
        {
            System.Collections.Generic.List<Salesperson> Salesperson = new System.Collections.Generic.List<Salesperson>()
        {
             new Salesperson {Name="Donald Duck ",  Quantity_Sold=173},
             new Salesperson {Name="Mickey Mouse ",  Quantity_Sold=202},
             new Salesperson {Name="Snoopy  ",  Quantity_Sold=203},
        };
      
        }
        private static void Bonus(ref int Quantity_Sold)
        {
            if (Quantity_Sold >= 200)
            {
                Console.WriteLine("Above 199 items");
            }
            else if (Quantity_Sold >= 100)
            {
                Console.WriteLine("100-199 items");
            }
            else if (Quantity_Sold >= 50)
            {
                Console.WriteLine("50-99 items");
            }
            else
            {
                Console.WriteLine("Below 50 items");
            }
            Console.WriteLine("Bonus Report: " Bonus);
        }
    }
}

I want a output that provides a report that looks like this but I get a blank output instead. enter image description here

The variables in an IF-Statement arent working

I´m trying to get some code working to upload some text to my database. The problem is that apparently my variable isnt passed throught to my IF-Statement. I literly tried everything. I hope you can help me. If you need more code snippets to work with im willing to give more.

I also use the jquery-tabledit from markcell. Github: https://github.com/markcell/jquery-tabledit

Here is the code of my index page:

    <?php 
                $id= $_GET["id"];
                $ser = $id."_ser";
                $wdh = $id."_wdh";
                $br  = $id."_br";
                $max  = $id."_max";
                $is  = $id."_is";
include_once("db_connect.php");
include("live_edit.php");
?>

And here is the code of my live_edit.php:

<?php
include_once("db_connect.php");
$input = filter_input_array(INPUT_POST);
if ($input['action'] == 'edit') {   
    $update_field='';
    if(isset($input[$ser])) {
        $update_field.= "$ser='".$input[$ser]."'";
    } else if(isset($input[$wdh])) {
        $update_field.= "$wdh='".$input[$wdh]."'";
        }
        else if(isset($input[$br])) {
            $update_field.= "$br='".$input[$br]."'";
            }
    if($update_field && $input['id']) {
        $sql_query = "UPDATE nameanddsc SET $update_field WHERE id='" . $input['id'] . "'"; 
        mysqli_query($conn, $sql_query) or die("database error:". mysqli_error($conn));     
    }
}
?>

Html Code of index.php

<?php 
                $id= $_GET["id"];
                $ser = $id."_ser";
                $wdh = $id."_wdh";
                $br  = $id."_br";
                $max  = $id."_max";
                $is  = $id."_is";
include_once("db_connect.php");
include("live_edit.php");
?>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<title>Sportarten ändern</title>
<script type="text/javascript" src="dist/jquery.tabledit.js"></script>
<?php include('container.php');?>    
<h1>Normale Aufgaben</h1>
    <table id="data_table" class="table table-striped">
        <thead>
            <tr>
                <th>Nummer</th>
                <th>Name</th>
                <th>Serie</th>
                <th>Wdhl</th>
                <th>Pause</th>
                <th>Maximal</th>
                <th>Ist-Wert</th>
            </tr>
        </thead>
        <tbody>
            <?php 
            
            $sql_query = "SELECT * FROM nameanddsc WHERE id <= 24 AND name!='' ORDER BY id";    
            $resultset = mysqli_query($conn, $sql_query) or die("database error:". mysqli_error($conn));
            while( $row = mysqli_fetch_assoc($resultset) ) {
            ?>
               <tr id="<?php echo $row ['id']; ?>">
               <td><?php echo $row ['id']; ?></td>
               <td><?php echo $row ['name']; ?></td>
               <td><?php echo $row [$ser]; ?></td>
               <td><?php echo $row [$wdh]; ?></td> 
               <td><?php echo $row [$br]; ?></td>
               <td><?php echo $row [$max]; ?></td>  
               <td><?php echo $row [$is]; ?></td>    
               </tr>
            <?php } ?>
        </tbody>
    </table>

        <h1>Zeitliche Aufgaben</h1>
        <table id="data_table1" class="table table-striped">
        <thead>
            <tr>
            <th>Nummer</th>
                <th>Name</th>
                <th>Serie</th>
                <th>Wdhl</th>
                <th>Pause</th>
                <th>Maximal</th>
                <th>Ist-Wert</th>
            </tr>
        </thead>
        <tbody>
            <?php 
            $sql_query = "SELECT * FROM nameanddsc WHERE id >= 25 AND name!='' ORDER BY id";        
            $resultset = mysqli_query($conn, $sql_query) or die("database error:". mysqli_error($conn));
            while( $row = mysqli_fetch_assoc($resultset) ) {
            ?>
               <tr id="<?php echo $row ['id']; ?>">
               <td><?php echo $row ['id']; ?></td>
               <td><?php echo $row ['name']; ?></td>
               <td><?php echo $row [$ser]; ?></td>
               <td><?php echo $row [$wdh]; ?></td> 
               <td><?php echo $row [$br]; ?></td>
               <td><?php echo $row [$max]; ?></td>  
               <td><?php echo $row [$is]; ?></td>
               </tr>
            <?php } ?>
        </tbody>
    </table>    

<script type="text/javascript">
$(document).ready(function(){
    $('#data_table').Tabledit({
        deleteButton: false,
        editButton: false,          
        columns: {
          identifier: [0, 'id'],                    
          editable: [[2, '<?php echo $ser; ?>'], 
          [3, '<?php echo $wdh; ?>'], 
          [4, '<?php echo $br; ?>']]
        },
        hideIdentifier: false,
        url: 'live_edit.php'        
    });
});
$(document).ready(function(){
    $('#data_table1').Tabledit({
        deleteButton: false,
        editButton: false,          
        columns: {
          identifier: [0, 'id'],                    
          editable: [[1, 'name'], [2, 'dsc']]
        },
        hideIdentifier: false,
        url: 'live_edit.php'        
    });
});
</script>

Formulating if-then constraints in linear binary programming

From a stock of various computer accessories of different brands, the optimization problem requires deciding to keep or discard products. I am struggling to formulate the following if-then conditions:

  1. If one product of a certain brand is eliminated than all the products of that brand should be eliminated.
  2. If any mouse (of any brand/model) is to be kept, then at least two different models of keyboards will also be kept.
  3. If any model of product X or product Y is to be kept, then all models of product Z of brand A will also be kept.

Create a lambda function with If inside a For Loop

I am working on some scraping on the web and I am trying to create a lambda function out of this.

The idea is that first I find all "td" in the BeautifulSoup variable I made (r_soup) and then I go deeper to search "a" and check the one that has "JPY" or "Depends on experience" in the text. That's the value I want:

salary = r_soup.find_all('td')
for s in salary:
    if s.find('a') and 'JPY' in s.text or s.find('a') and 'Depends on experience' in s.text:
        print(s.text.strip())

I have tried this:

salary = list(map(lambda s: s.text if (s.find('a') and 'JPY' in s.text) or (s.find('a') and 'Depends on experience') in s.text ,r_soup.find_all('td')))
salary

But it is not working. I don't know much about lambda functions and I have been searching on the web but to no avail. Thanks a bunch for the help!