dimanche 2 septembre 2018

Incorrect if-else statements

I've been trying to write a function that will ask for a number between 1-11 and will randomly choose a number and will compare between them. I can't figure out why no matter what number I type in (equals or smaller or bigger) it will always type the "Your number is less" message.

def loto():
    _number = int(input("Enter any number between 1-10: "))
    import random
    for x in range(1):
        print ("Python chose: " + str(random.randint(1,11)))
    if ("_number" == "random"):
        print ("You Won! :)")
    elif ("_number" < "random"):
        print ("Your number is less")
    elif ("_number" > "random"):
        print ("Your number is more")
    else:
        print ("You Lost :(")

loto()

I'm using Python 3.

Thanks:)

main loop function doesn't run nested functions

I'm new in the JavaScript and programming world and I'm struggling with a basic JavaScript exercise, I should create with just basic functions a times table based on a user prompt value, it should a loop that interrupts just when the user insert the value -1. Although, the loop doesn't print the times table until I interrupt the loop with the -1, it seems like it's looping too fast.

Here he's my code:

var userInput;

function checkValidity(question) {

  if(isNaN(question) || (question === "" )) {
    console.log('Inserire un numero valido');
    return false;
  } else {
    console.log('defined');
    return true;
  }
}

function timeTable(valueUser) {
  document.write("Times Table for number: " + valueUser + "<br />");
  
  for(var i = 0; i < 10; i++) {                                      
    document.write(valueUser + " * " + i + " = " + valueUser * i + "<br />");
  }

}

function showQuestion() {
  userInput = parseInt(prompt("Enter your Times Table value:"));
  
  var answerValidate = checkValidity(userInput);

  if(answerValidate) {
    if(userInput !== -1) {
      timeTable(userInput);
      showQuestion();
    } else {
      console.log("Grazie per aver partecipato!");
    }
  } else {
    showQuestion();
  }

  
}

showQuestion();

Anyone that can suggest me in what I'm wrong?

Thank you everyone!

IF contains with multiple other conditions

this is from a none programmer I have the formula below that checks 3 conditions

=IF((AND(H2="DWL",K2<=0,W2<>"")), "Send Email", "Wait")
H2= DWL
K2 is Less than 1
W2 is occupied  

But I am trying to get so if H2 contains “DWL” plus the other 2 criteria’s are correct

Eg, H2 may contain “DWL1234”, “DWL5678” “DWL25643” or “DWLJHRER”

Python dual condition in if statement

Im trying to satisfy 2 conditions in the if statement with the word AND but it does not seem to be working. is there something wrong? Im basically trying to print the largest of e1,e2 and e3.

if e1 > e2 AND e3: then print elapsed 1 elif: e2>e3 then print e2 else: print e3

ifelse() and paste0() in a vector

I've got a vector (mychipvalue) with a single numeric element in it.

If the numeric is 0, I'd like to replace it with "<0.001". If the numeric is not 0 but eg 0.053 I'd like to leave it untouched (keep 0.053).

Thanks for any help!

mychipvalue <- 0 
mychipvalue < ifelse(mychipvalue==0,paste0("<0.001"),paste0(mychipvalue))

Adding two numbers by c program

#include

INT main

{

INT d,a=1,b=2;

d=a++ + ++b;

printf("%d %d %d",d,a,b);

What is the output of d,a,b?

How can I convert this if statement to an arrow function?

I have an function with an if statement that says:

function reflect() {
    if (number_a >= 600) {
         ball_velocity_a = -ball_velocity_a }
    if (number_b >= 600) {
         ball_velocity_b = -ball_velocity_b
}}

and I'm trying to convert this function into arrow function

const deflect = () => number_a >= 600 ? ball_velocity_a = -ball_velocity_a : 

but I'm not sure what to put after the : as ball_velocity_b is conditioned with number_b instead of number_a. I'm really new to arrow functions and would appreciate some help on this.