How does my input not equal the answer?
I am assuming you are using python3.
The only problem with your code is that the value you get from input()
is a string and not a integer. So you need to convert that.
string_input = input('Question?')
try:
integer_input = int(string_input)
except ValueError:
print('Please enter a valid number')
Now you have the input as a integer and you can compare it to a
Edited Code:
import random
import operator
ops = {
'+':operator.add,
'-':operator.sub
}
def generateQuestion():
x = random.randint(1, 10)
y = random.randint(1, 10)
op = random.choice(list(ops.keys()))
a = ops.get(op)(x,y)
print("What is {} {} {}?\n".format(x, op, y))
return a
def askQuestion(a):
# you get the user input, it will be a string. eg: "5"
guess = input("")
# now you need to get the integer
# the user can input everything but we cant convert everything to an integer so we use a try/except
try:
integer_input = int(guess)
except ValueError:
# if the user input was "this is a text" it would not be a valid number so the exception part is executed
print('Please enter a valid number')
# if the code in a function comes to a return it will end the function
return
if integer_input == a:
print("Correct!")
else:
print("Wrong, the answer is",a)
askQuestion(generateQuestion())
Python : How to check if input is not equal to any item in a 2d array?
You're very close!
What you're trying to do: break out of the loop if there are any names in user_list that match the new name.
What it's currently doing: breaking out of the loop if there are any names in user_list that don't match the new name.
I.e., if you enter Daniel, since Daniel != Raymond, you will break early.
Instead, what you should do is break if the newly entered name is not present in a list of names:
user_list = [['usr1','Daniel'],['usr2','Raymond'],['usr3','Emanuel']]
name = input("Please enter your name : ")
while True:
if name == '':
name = input("Please enter your name : ")
else:
if name in [user[1] for user in user_list]: # existing names list
print("This username has been registered")
name = input("Please try another username : ")
else:
break
print("Username registered as",name)
In python, how do I repeat a prompt for input if the input does not equal 1-4?
input is returning a string and you are comparing it to an int.
userInput = 0
while userInput not in [1, 2, 3, 4]:
userInput = int(input('Enter a number'))
Why does it seem like the strings are not equal?
Reason why this
if (strcmp(a,b) == 0) { }
is not true because fgets()
stores the \n
at the end of buffer. So here array a
looks like house
and array b
looks like house\n
(If ENTER
key was pressed after entering input char's) and strcmp(a,b)
doesn't return 0
.
From the manual page of fgets()
fgets()
reads in at most one less than size characters from stream
and stores them into the buffer pointed to by s. Reading stops
after an EOF or a newline. If a newline is read, it is stored
into the buffer. A terminating null byte ('\0') is stored after
the last character in the buffer.
One way is to use strcspn()
which removes the trailing \n
. For e.g
fgets(a,n,stdin);
a[strcspn(a, "\n")] = 0;
Now compare the char array like
if (strcmp(a,b) == 0) {
printf("The strings are equal.\n");
}
else {
printf("The strings are not equal.\n");
}
Input and output do not equal in jQuery function
If you use the keyup event, it works as you desire. I believe this is because the keydown event fires before the value is populated into the text field, so it cannot be used.
How do I test if a variable does not equal either of two values?
Think of !
(negation operator) as "not", ||
(boolean-or operator) as "or" and &&
(boolean-and operator) as "and". See Operators and Operator Precedence.
Thus:
if(!(a || b)) {
// means neither a nor b
}
However, using De Morgan's Law, it could be written as:
if(!a && !b) {
// is not a and is not b
}
a
and b
above can be any expression (such as test == 'B'
or whatever it needs to be).
Once again, if test == 'A'
and test == 'B'
, are the expressions, note the expansion of the 1st form:
// if(!(a || b))
if(!((test == 'A') || (test == 'B')))
// or more simply, removing the inner parenthesis as
// || and && have a lower precedence than comparison and negation operators
if(!(test == 'A' || test == 'B'))
// and using DeMorgan's, we can turn this into
// this is the same as substituting into if(!a && !b)
if(!(test == 'A') && !(test == 'B'))
// and this can be simplified as !(x == y) is the same as (x != y)
if(test != 'A' && test != 'B')
i made it while input does not equal q or quit but when i put in q or quit
while shape != "q" or shape != "quit":
is causing issues. If your string is q
, it's not equal to quit
, so the loop will keep running - and vice versa. Use this instead:
while shape != "q" and shape != "quit":
Now, if either of these conditions fail, the while loop will break.
Related Topics
How Does Python's "Super" Do the Right Thing
Cx_Freeze Crashing Python 3.7.0
How to Select Rows with One or More Nulls from a Pandas Dataframe Without Listing Columns Explicitly
How to Read a Column of CSV as Dtype List Using Pandas
How to Copy Inmemoryuploadedfile Object to Disk
Finding Duplicate Files and Removing Them
About the Pil Error -- Ioerror: Decoder Zip Not Available
Why Do Two Identical Lists Have a Different Memory Footprint
Test Case Execution Order in Pytest
Get First Row Value of a Given Column
Error When Loading Cookies into a Python Request Session
What Is the Meaning of "Failed Building Wheel for X" in Pip Install
Get Column Index from Column Name in Python Pandas
Mixing Cdef and Regular Python Attributes in Cdef Class