mercredi 1 décembre 2021

Bmi calculator if statements do not work in python

I am stuck writing a program to calculate bmi and categorizing them.

individuals = list()
for i in range(6):
    user = str(input("Please enter the names of the 6 users who want to calculate their BMI: "))
    individuals.append(user)
BMIs = []
for user in individuals:
    print("Calculating for", user)
    height = int(input(user + ", in inches, how tall are you? "))
    weight = int(input(user + ", in pounds, how much do you weight? "))
    BMIs.append(user  + ", your BMI is: " + str(weight * 703/height**2))

for BMI in BMIs:
    print(BMI)
    
if  int(BMI)< 16:
   print( user + "BMI is:" + str(bmi) + "and you are:severely underweight")

elif int(BMI)>= 16 and bmi < 18.5:
   print( user + "BMI is:" + str(bmi) + "and you are:severely underweight")

elif int(BMI) >= 18.5 and bmi < 25:
   print( user + "BMI is:" + str(bmi) + "and you are:severely Healthy")

elif int(BMI)>= 25 and bmi < 30:
   print( user + "BMI is:" + str(bmi) + "and you are:severely overweight")

elif int(BMI) >=30:
   print( user +" " + "BMI is:" + str(bmi) + "and you are:severely overweight")

the if statements dont work and i cant figure how to categorize those who are overweight, healthy

how to write cleaner if-else statements and prevent nesting?

So I know that nesting code can get ugly very quickly. Therfore I looked up a way to prevent nesting an found out that you can do the following

if (user == null) return;

console.log('Deleting');
user.delete();

Instead of using curly brackezs like that

if (user != null) {
console.log('Deleting');
user.delete();
}

However I can't really see how this is helpful when using an if-else statement. For example: I have this piece of code from my project that I would really like to write in a cleaner way than that. But I am struggling to figure out how this can be done.

           if (parseInt(year) == joinTime.getFullYear()) {
                if (parseInt(month) == joinTime.getMonth() + 1) {
                    if (parseInt(day) == joinTime.getDay()) {
                        channel.send(message);
                    } else {
                        comparison(day, hour, minute);
                    }
                } else {
                    comparison(day, hour, minute);
                }
            } else {
                comparison(day, hour, minute);
            }

Powershell- Write-Output not working within 'else'

So, I'm new to powershell, so just bear with me here.

I'm using powershell to create a command prompt, and most of it, is just functions, and if statments, when i try to use a 'else' statement, with 'Write-Output', it leaves a blank new line, heres my code;

while ($true) {
$Title = "Falcon Os v1 alpha cli"
$host.UI.RawUI.WindowTitle = $Title
$global:current_path=Get-Location
$global:current_path=$global:current_path.Path
$starterterminalpromptone = $current_path
$starterterminalprompttwo = " >>> "
$prompt = $starterterminalpromptone + ". Type 'sys.win.man.help' for help " + $starterterminalprompttwo
Write-Host $prompt -NoNewLine
$command = $Host.UI.ReadLine()
if ($command -eq "sys.win.info.nl") {sys.win.info.nl}
if ($command -eq "sys.win.man.help") {sys.win.man.help}
if ($command -eq "sys.command.output.out.echo.command.variable") {sys.command.output.out.echo.command.variable}
if ($command -eq "sys.exit") {break}
if ($command -eq "sys.path.cd") {sys.path.cd}
if ($command -eq "sys.path.view") {sys.path.view}
if ($command -eq "sys.screen.cls") {cls}
if ($command -eq "sys.path.mkdir") {sys.path.mkdir}
if ($command -eq "sys.path.makefile") {sys.path.makefile}
if ($command -eq "sys.win.cleartext.vtext.show") {sys.win.cleartext.vtext.show}
elseif ($cs -eq "start"){Write-Output ""}
elseif($cs -eq "command"){Write-Output ""}
else {Write-Output "Unknown command."}
}

By the way, all my if statemnts lead to mostly functionns, but made it the only part i need.

What it does when i put a unknown command, it simply leaves a blank new line.

I use powershel 7.20, and use ISE to edit my code.

how input char array works while using in if condition [closed]

I tried following code but I didn't getting desire output

char a[50];

char b[50] = "abc";

std::cin << a ;

if(a == b){

std::cout << "Matched!!" << std::endl;

}else{

std::cout << "Not Matched!!" << std::endl;

}

I'm getting "Not Matched!!" while I put input as "abc". Why this happend?

if statement in public void won't run

It maybe a very stupid question, but my public void with if statement doesn't run and I just can't find the answer why. I tried debugging, looking for similar topics in the web, but nothing helped. The funny thing in a video course which I am currently studying where a similar task is the codes runs although there are some differences which aren't relevant.

public class Program
{
    static int InputYear;
    static int YearDiv2;
    static int YearDiv4;
    static int YearDiv100;
    static int YearDiv400;
    string Yes = "Yes";
    string No = "No";
    string DivBy = "Divisible by ";
    string LeapYear = "Leap Year: ";


    public static void Main(string[] args)
    {
        //Section 5 Task

        Console.WriteLine("Please enter a year value:");
        InputYear = Convert.ToInt32(Console.ReadLine());
        YearDiv2 = InputYear % 2;
        //Console.WriteLine(YearDiv2);
        YearDiv4 = InputYear % 4;
        YearDiv100 = InputYear % 100;
        YearDiv400 = InputYear % 400;

        Console.WriteLine($"Year entered: {InputYear}");

    }
    public void YearNotOdd()
    {
        Console.WriteLine("It runs");
        if (InputYear == 0)
        {
            
            if (YearDiv4 == 0 && YearDiv100 == 0 && YearDiv400 == 0)
            {
                Console.WriteLine(@"{DivBy} 4: {Yes}");
                Console.WriteLine(@"{DivBy} 100: {Yes}");
                Console.WriteLine(@"{DivBy} 400: {Yes}");
                Console.WriteLine(LeapYear + Yes);
            }

            else if(YearDiv4 == 0 && YearDiv100 == 0 && YearDiv400 != 0)
            {
                Console.WriteLine(@"{DivBy} 4: {Yes}");
                Console.WriteLine(@"{DivBy} 100: {Yes}");
                Console.WriteLine(@"{DivBy} 400: {No}");
                Console.WriteLine(LeapYear + No);
            }

            else
            {
                Console.WriteLine(@"{DivBy} 4: {No}");
                Console.WriteLine(@"{DivBy} 100: {No}");
                Console.WriteLine(@"{DivBy} 400: {No}");
                Console.WriteLine(LeapYear + No);
            }


        }
        else
        {
            Console.WriteLine(@"{DivBy} 4: {No}");
            Console.WriteLine(@"{DivBy} 100: {No}");
            Console.WriteLine(@"{DivBy} 400: {No}");
            Console.WriteLine(LeapYear + No);
        }
    }
}

Prevent Intellij IDEA from indenting my comment on else statement

Usually I write comment on else statement like this:

if (someStatements) {
    do();
// comment on else statement
} else if (someOtherStatements) {
    do();
// another comment on else statement
} else {
    do();
}

But after IDEA rearranges code I got comments indented:

if (someStatements) {
    do();
    // comment on else statement
} else if (someOtherStatements) {
    do();
    // another comment on else statement
} else {
    do();
}

How do I prevent IDEA code rearranging from intending comments? Or how to tell IDEA just dont rearrange my comments?

mardi 30 novembre 2021

java: too many cases makes performance drop [closed]

So for example I have code like this

String input;

switch(input){
   case "a": // do a process break;
   case "b": // do a process break;
   ... until around 40 functions
}

each process is unique. Simple example of the file

a
b
c
a
b

so the problem with performance is like this I use that function to read a file (that function run 20000 times).

when I run switch for 30 cases (comment 10 cases) the time is (assume) 30s
when I run switch for 35 cases (comment 5 cases) the time become 50s
when I run switch for 40 cases the time also become 50s
all of the 3 use same input that 10 commented cases is not exist in input

I am curious why there is a jump in that processing time and actually I also tried by using 30 cases which is 30s I added this code

// after the code above    
boolean temp = false;
if(temp){ // do nothing }

and the processing time is jump into 50s so the processing time is always jump from 30s to 50s when I add any if function except if(true){} or if (false){} but after it jump into 50s, it become stable in 50ish again

I wonder if java has some kind of memory allocation that affect this behaviour I tried to increase heap memory -Xmx but the result is still the same