if-statement

Python - How to break while loop after empty value in a int turning input? [duplicate]

≡放荡痞女 提交于 2020-07-04 03:22:31
问题 This question already has answers here : Asking the user for input until they give a valid response (20 answers) Closed 3 years ago . I want to make the while loop break when empty input is typed. I know that the error is due to the int function because it cant turn to integer an empty input but how can i do what i'm trying to write? while True: numb = int(input("number")) if numb % 2 == 0: print("this number is even") elif numb % 2 != 0: print("this number is odd") elif numb == '': break 回答1

Python - How to break while loop after empty value in a int turning input? [duplicate]

大城市里の小女人 提交于 2020-07-04 03:21:13
问题 This question already has answers here : Asking the user for input until they give a valid response (20 answers) Closed 3 years ago . I want to make the while loop break when empty input is typed. I know that the error is due to the int function because it cant turn to integer an empty input but how can i do what i'm trying to write? while True: numb = int(input("number")) if numb % 2 == 0: print("this number is even") elif numb % 2 != 0: print("this number is odd") elif numb == '': break 回答1

Shorten nested ifelse

梦想的初衷 提交于 2020-07-02 20:32:15
问题 If the following data table is given, and we would like to compare x1 consequently with x2 to x5, the following can be used: set.seed(1) library(data.table) TDT <- data.table(x1 = round(rnorm(100,0.75,0.3),2), x2 = round(rnorm(100,0.75,0.3),2), x3 = round(rnorm(100,0.75,0.3),2), x4 = round(rnorm(100,0.75,0.3),2), x5 = round(rnorm(100,0.75,0.3),2)) TDT[,compare := ifelse(x1 < x2,1,ifelse(x1 < x3,2,ifelse(x1 < x4,3,ifelse(x1 < x5,4,5))))] So if x1 < x2 , then compare == 1 , etc. Now in my

sum of rows when condition is met- data.frame in R

空扰寡人 提交于 2020-06-29 03:40:44
问题 df var1 var2 1 a 1 2 b 2 3 a 3 4 c 6 5 d 88 6 b 0 df2 <- data.frame(var1=c("k","b","a","k","k","b"),var2=c(14,78,5,6,88,0)) > list <- list(df,df2) for(i in list){ if(any(i[ ,1] == i[ ,1})){ cumsum(.) } } I have a list containing of data.frames. I want to iterate over these data.frames. When there is the same letter in the first column, then the sum should be calculated. I want this new row to be in my data.frame. I completely messed up the if statement . Can somebody help me please? EDIT: the

Return cell content based on max value of other column in Google Sheets

橙三吉。 提交于 2020-06-27 18:55:10
问题 In Google Sheets, I have a table with dynamic cells that count the number of occurrences in a feed from Google Forms. At the left, in Column A there are names of items, and the columns at the right count how many times they are listed in the responses from the forms, so this values change as more responses are added. I am trying to make a report that mentions which item had the most instances per column. The formula I used initially works: =INDEX(INDIRECT("A$3:A$6"), 1, MATCH(MAX(B3:B6), B3

Declare variable in if statement (ANSI C)

不问归期 提交于 2020-06-25 06:41:49
问题 Is there any way to declare variable in if statement (using ANSI C only) ? Example: if(int variable = some_function()) { return 1; } 回答1: No, you cannot do that. What you can do is create a block just for the if { int variable; variable = some_function(); if (variable) return 1; } /* variable is out of scope here */ Note that for this simple case you can call the function as the condition of the if (no need for an extra variable) if (some_function()) return 1; 来源: https://stackoverflow.com

Alternative to multiple if else statements in JavaScript?

大兔子大兔子 提交于 2020-06-25 06:37:08
问题 I have an input which contains a dropdown list of 8 items. Depending on the option the user picks, I want to change the value of their input into a a different string value. In order to do this, I am using a ton of if else statements, which make this look very bulky and I would like to condense this if at all possible. I have the following code: if (inputFive == "Corporation"){ inputFive = "534" } else if (inputFive == "LLC"){ inputFive = "535" } else if(inputFive == "LLP"){ inputFive = "536"

Change a dynamic's type in a ternary conditional statement

与世无争的帅哥 提交于 2020-06-23 13:39:10
问题 In C#, the type dynamic allows you to change a variable's type at runtime, for example: dynamic x = "foo"; x = 42; Another example: dynamic x; if (true) x = "foo"; else x = 42; However, when using the shorthand "?:" ternary conditional statement, dynamic x = (true) ? "foo" : 42; will not compile: error CS0173: Type of conditional expression cannot be determined because there is no implicit conversion between 'string' and 'int' Why is that so? 回答1: The specification has this to say about how

Applying mutate_at conditionally to specific rows in a dataframe in R

别等时光非礼了梦想. 提交于 2020-06-23 04:11:09
问题 I have a dataframe in R that looks like the following: a b c condition 1 4 2 acap 2 3 1 acap 2 4 3 acap 5 6 8 ncap 5 7 6 ncap 8 7 6 ncap I am trying to recode the values in columns a, b, and c for condition ncap (and also 2 other conditions not pictured here) while leaving the values for acap alone. The following code works when applied to the first 3 columns. I am trying to figure out how I can apply this only to rows that I specify by condition while keeping everything in the same dataframe

Alternative to nested ifelse() statements in R

房东的猫 提交于 2020-06-17 03:30:26
问题 Problem I need a function which computes uncertainty of measurement by an instrument for different ranges (eg. I measure electrical current and if it's in the range of 2 mA the uncertainty is 0.1 % + 3 dig of the measured value). It is better if the function is able to take a vector and return a vector instead of just numbers. I have written the function with lots of if s but it returns warnings the condition has length > 1 and only the first element will be used . After a while of research I