Checking if two variables are equal
how to compare two variables in python
python if two values are equal
r check if two vectors are identical
python not equal
python check if two objects are the same instance
python if multiple values
In my code I'm making a basic multiplying game.
But in my game,
When you get the answer right, it says you got it wrong
Here's my whole code:
import random score = 0 while True: num1 = random.choice(range(1,12)) num2 = random.choice(range(1,12)) answer = num1 * num2 a = input("Solve for " + str(num1) + "x" + str(num2)) if a == answer: print("congrats you got it right") score += 1 else: print("Wrong sorry play again") print("Score was: " + str(score)) break
When I get the right answer I get
Solve for 7x10 70 Wrong sorry play again Score was: 0
Other languages might let you get away with this, but Python is strongly typed. The input function gets a string, not a number. Numbers can't be equal to strings. Either convert the number to a string or the string to a number before you compare them. You can use
int to convert.
Checking if two variables are equal, You can compare 2 variables in an if statement using the == operator. examplea = 10 b = 15 if a == b: print(Equal) else: print(Not If you've got static strings, this is very easy to check via regular expressions: If you've got a small set of values (string or number), you can use a switch: If you've got a long list of values, you should probably use an array with ~arr.indexOf(item), or arr.contains(item):
input returns what was typed as a string... in order to compare it with the answer, you need to either convert it to
if int(a) == answer:
or the other way around (convert answer to
if a == str(answer):
The first one may raise an exception if
a is not parseable to an
Here the docs.
PS: I really wonder how ur random library picked a 1070 sampling from 0 to 11...
How to compare two variables in an if statement using Python?, The algorithm to compare two strings is simple: Compare the first If both strings end at the same length, then they are equal. Otherwise, the ErrorTypeFV_28, one for each year and each can have 4 values, 1,2,3, or 4. Basically what I want to check is that for all rows, if all ErrorTypeFV_1 through ErrorTypeFV_28 are EQUAL, then to set a new variable, "TYPE" to the value of these variables. The issue is that some of these years 1-28 can be blank,
import random score = 0 while True: num1 = random.choice(range(1,12)) num2 = random.choice(range(1,12)) answer = num1 * num2 a = int(input("Solve for " + str(num1) + "x" + str(num2))) if a == answer: print("congrats you got it right") score += 1 else: print("Wrong sorry play again") print("Score was: " + str(score)) break
Comparisons, The safe and reliable way to test two objects for being exactly equal. Character strings are regarded as identical if they are in different marked encodings but To check if two strings are equal in bash scripting, use bash if statement and double equal to== operator. To check if two strings are not equal in bash scripting, use bash if statement and not equal to!= operator.
Test Objects for Exact Equality, This tests whether two terms are identical. It does Now let's look at examples involving variables, and explicitly compare == with the unification predicate = . ?- When you use or, python sees each side of the operator as separate expressions. The expression x or y == 1 is treated as first a boolean test for x, then if that is False, the expression y == 1 is tested. This is due to operator precedence. The or operator has a lower precedence than the == test, so the latter is evaluated first.
9.1 Comparing terms, Brief: This example will help you to understand to check if two strings are equal in a bash script. This shell script accepts two string in variables As we all know, to compare if two cells are equal, we can use the formula A1=B1. But, if you want to check if multiple cells have the same value, this formula will not work. Today, I will talk about some formulas to compare if multiple cells are equal in Excel.
Bash - Check if Two Strings are Equal?, The safe and reliable way to test two objects for being exactly equal. Character strings are regarded as identical if they are in different marked encodings but It is not clear what it means to test if two data frames are "value equal" but to test if the values are the same, here is an example of two non-identical dataframes with equal values: a <- data.frame(x = 1:10) b <- data.frame(y = 1:10) To test if all values are equal: all(a == b) # TRUE
- When I change it to int I get a = input("Solve for " + int(num1) + "x" + int(num2)) TypeError: must be str, not int
- I have fixed it, I made it str(a) == str(answer):
- I also wondered that but the case is 7 and 10 is random picked integer and 70 is the input provided to program there is no space thatsy its looking like the number is 1070
- oh i see! you could add
+ " = "to your input ;)