jeudi 28 janvier 2021

How to access a variable inside of an if-statement

This is a section of my function where if the scores were shifted (1) then it prints "Shift amount?" after and collects user input to be used in the compute_total(earned,shifted, shift) function:

printf("Were scores shifted (1=yes, 2=no)? ");
    double shifted;
    scanf("%lf", &shifted);
    if(shifted == 1){
      printf("Shift amount? ");
      double(shift);
      scanf("%lf", &shift);
    }else{}

    double total = compute_total(earned, shifted, shift);

However, obviously I cannot access the variable inside of the if-statement, when I bring the variable and scanf for shift outside of the if scope then the program will run but as I enter a digit for shift amount the terminal does not go any further through my program, it gets "stuck".

How should I solve this issue so I can access the variable but still function correctly?

Assigninga function to an IF statement

It's been a while since I used python (been studying CISCO IOS) so I'm bit rusty. I have forgotten how to assign a function to an IF statement.

import ipaddress

q1= input("Please enter 'network' to calculate an IP address range, or enter 'port' to scan an ip address for open ports  ")
if (q1 == str(network)):
    networkclac()
if (q1 == str(port)):
    portscan()


def networkclac():
    try:
        network = input('please input network address: ')
        network = ipaddress.ip_network(network)
        print(network)

    except ValueError:
        print('That is not a network address')

    iplist= list(ipaddress.ip_network(network).hosts())
    for i in range(10,len(iplist),2):
        print(iplist[i])

#def portscan():

Output

Traceback (most recent call last):
  File "C:\Users\shane\PycharmProjects\RodCertIV\ITCNWK409.py", line 4, in <module>
    if (q1 == str(network)):
NameError: name 'network' is not defined

Process finished with exit code 1

The aim of the program is to calculate a range of IP addresses, and scan ip addresses for open ports.

I have created the code for the IP Calculator, but I have forgotten how to call up a function in an IF statement.

I want the program, to ask the user if they wish to calculate an IP network address or run a port scan. If the end user inputs "network" I want the program to run the def network clac(): function

It's something quite simple that I'm missing, this should be an easy question for some.

Python if test condition fails when used with or

Greetings python experts. I had written an if condition as follows that fails to be false for objects that should be false. I am writing in python 3.8.5. Note instance_list in this example contains a list of resources that are in various states. I only want to append vm_instance_list with resources that are not in a TERMINATED or TERMINATING state.

instance_results = compute_client.list_instances(
    compartment_id = compartment_id).data
vm_instance_list = []
for instance in instance_results:
    if instance.lifecycle_state != "TERMINATED" or instance.lifecycle_state != "TERMINATING:
        vm_instance_list.append(instance)

The above code appends vm_instance_list with every object in the list instance_results, aka each condition is interpreted as True for objects that are in a TERMINATED or TERMINATING lifecycle state. I have had to re-write to nest the if conditions, which works.

for instance in instance_results:
    if instance.lifecycle_state != "TERMINATED:
        if instance.lifecycle_state != "TERMINATING":
            vm_instance_list.append(instance)

I have no idea what why I have had to nest the above if statements and would appreciate if anyone could share some insights.

Thanks so much,

  • Hank

Cant get my java class to display correct values (game) (getting all random damage to work, along with health)

I'm new to java and trying my hand at making a game. Here is what I have so far in my "combat class." Right now, if this was displayed with:

Combat fight1 = new Combat("Homeless Man", "Rusty Fork", 100, 110, 30); 

I would get the output:

Homeless Man hits you with a Rusty Fork, Dealing 0 damage!
You now have 0 HP left!
You hit Homeless Man With an attack using your null dealing 0 damage!
Homeless Man now has 100 HP left! 

Is there any way that I can fix this to make it display the right values (random damage, health values, damage values, etc.)

I know that this is a lot of code (and work) to look through, so all help is greatly appreciated!

The code for the "Combat" class is below:

package Walking;
import java.util.Random;
import java.util.Scanner;

public class Combat {

//enemy
String enemyName;
String enemyItem;
int enemyHP;
int enemyMaxDam;
int enemyMinDam;

public int damageDelt;

//player
public int playerHP;
public int playerMaxDam;
public int playerMinDam;

public int damageTaken;

//player armor
int clothes = 1;
int tux = 0;

//player weapon
String playerWeapon;
int fists = 1;
int bloodySpoon = 0;


public Combat() {
    if(clothes == 1) {
        playerHP = 1000;
    }else {
        if(tux == 1) {
            playerHP = 1100;
        }
    }
    
    if(fists == 1) {
        playerMaxDam = 75;
        playerMinDam = 30;
    }else {
        if(bloodySpoon == 1) {
            playerMaxDam = 100;
            playerMinDam = 75;
        }
    }
    
}

public Combat(String _enemyName, String _enemyItem, int _enemyHP, int _enemyMaxDam, int _enemyMinDam) {
    enemyName = _enemyName;
    enemyItem = _enemyItem; 
    enemyHP = _enemyHP;
    enemyMaxDam = _enemyMaxDam;
    enemyMinDam = _enemyMinDam;     
}

public int getDamageDelt() {
    Random rand = new Random();
    damageDelt = rand.nextInt(playerMaxDam = playerMinDam + 1) + playerMinDam;
    return damageDelt;
}

public int getDamageTaken() {
    Random rand = new Random();
    damageTaken = rand.nextInt(enemyMaxDam - enemyMinDam + 1) + enemyMinDam;
    return damageTaken;
}

public void displayCombat() {
    
    Scanner in = new Scanner(System.in);
    System.out.println("Will you:\n -Attack (1)\n -Consume Item (2) \n -Run (3)");
    String userInput = in.nextLine();
    if(userInput.equals("1")) {
        System.out.println(enemyName + " hits you with a " + enemyItem + ", Dealing " + damageTaken + " damage!");
        System.out.println("You now have " + (playerHP - damageDelt) + " HP left!");
        System.out.println("You hit " + enemyName + " With an attack using your " + playerWeapon + " dealing " + damageDelt + " damage!");
        System.out.println(enemyName + " now has " + (enemyHP - damageDelt) + " HP left!");
        
    }else {
        if(userInput.equals("3")) {
            System.out.println("You managed to escape the fight with " + playerHP + " left!");
        }
    }
    
}
}

Change Text content single Class but multiple instances

So. I'm currently trying to insert text content that may change depending of image validation using a single class for multiple divs. Any help is appreciate!

HTML

...
<div id="trophies"><img id="trophyimage" src="{$host}/trophies/$UserTR" height="100" width="100">
<span id="text-content" class="spaner"></span></div>

Right now using the next Javascript its achieving what I want, but it only does it once per ".spanner" class, not in the rest.

JavScript

 var trophy = document.getElementById("trophyimage");
            if(trophy.src == "...//user/trophies/A.png"){
                     var x = document.getElementsByClassName("spaner")[0]; 
    x.textContent = "Trophy A";
                    }
                else if (trophy.src == "...//user/trophies/B.png"){
                    var x = document.getElementsByClassName("spaner")[0];
    x.textContent = "Trophy B";
    }
    else{ var x = document.getElementsByClassName("spaner");
    x.textContent = "Null";
    }

I'm trying to figure out how to make it work using something like this:

JavaScript

var trophiestext = Array.from(document.querySelectorAll("spaner"));
    trophiestext.forEach(function(troph) {
        var trophy = document.getElementById("trophyimage");
            if(trophy.src == "...//user/trophies/A.png"){
                     var x = document.getElementsByClassName("spaner"); 
    x.textContent = "Trophy A";
                    }
                else if (trophy.src == "...//user/trophies/B.png"){
                    var x = document.getElementsByClassName("spaner");
    x.textContent = "Trophy B";
    }
    else{ var x = document.getElementsByClassName("spaner");
    x.textContent = "Null";
    }

Change Text Content using same class javascript

I'm still very new to Javascript and working in different scenarios, sorry for anything you may see that could hurt your eyes!

So. I'm currently trying to insert text content that may change depending of image validation using a single class for multiple divs. Any help is appreciate!

JavaScript

var trophiestext = Array.from(document.querySelectorAll("spaner"));
    trophiestext.forEach(function(troph) {
        var trophy = document.getElementById("trophyimage");
            if(trophy.src == "...//user/trophies/A.png"){
                     var x = document.getElementsByClassName("spaner"); 
    x.textContent = "Trophy A";
                    }
                else if (trophy.src == "...//user/trophies/B.png"){
                    var x = document.getElementsByClassName("spaner");
    x.textContent = "Trophy B";
    }
    else{ var x = document.getElementsByClassName("spaner");
    x.textContent = "Null";
    }

HTML

...
<div id="trophies"><img id="trophyimage" src="{$host}/trophies/$UserTR" height="100" width="100">
<span id="spaner" class="text-content"></span></div>

Finding the biggest number from one-randomly generated and two-prompted in JavaScript

everyone. I'm new in coding and trying to make my way through..

I have a task to write a program, that will find the biggest number from 3 numbers and will display it with certain conditions.

  1. If the number is bigger than 70, then it should be reduced on 10% and if it bigger than 40, reduced on 5% if it bigger than 10, reduced on 2%. enter image description here