0
am not sure how this work but it gave me the output I needed
age = 18 name = "Girly" #newAge = int(input("Guess the system age: ")) #newName = str(input("Guess the system name: ")) if not age and not name: print("Found False") else: print("Default True") I commented out line 3 and 4 because I wanted to receive user input in the condition but not working out. how can I use user input for booleans
13 Respostas
+ 5
Woah! well I'm glad it is, was afraid I might misunderstood you again XD
+ 8
I'm not sure I understand your question here, but if you want to branch to the "if" part rather than the "else" you can try to use predefined invalid value for the variables, for example set age to 0, and name to "" (empty string), that way it will output "Found False".
Sorry if it's not the answer you're looking for, you can give more explanation to what you want to do to be more descriptive, if you wish : )
Hth, cmiiw
+ 6
If you use "not" it means both the age & name comparison must both give False so we get into the "if" part (not False = True), as for me, I'm used to put parentheses around the expressions to check before I put it to face the "not", that way I ensure that age and name comparison is evaluated first;
if not (systemAge == userInput_age and systemName == userInput_name):
Why suddenly change your mind on it mate? if I may ask...
+ 5
I'm guessing you want to compare whether if newAge equals age, and newName equals name, if that's what you mean, you can try this:
age = 18
name = "Girly"
newAge = int(input("Guess the age: "))
print(newAge)
newName = input("Guess the name: ")
print(newName)
if age!=newAge or name!=newName:
print("Beep! wrong answer, try again!")
else:
print("Yeah! that's right!")
I hope this time I understand you correctly, if still not, just say it, okay : )
+ 5
Hmm okay, but let's see first which part of it that's troubling you? I'll try best to explain it, I'm actually still learning Python too...
+ 5
@Adison, yes multiple different boolean operators are allowed, depending on how you wish the outcome would be, and how many expressions are there to compare.
The use of "not" in an "if" is mostly used to catch a False result, I mean when you want to place the code that handles False inside the "if" part, then we use "not":
if not okay:
# code for not okay
else:
# code for okay
On the contrary,
if okay:
# code for okay
else:
# code for not okay
In your case, okay is a condition where age match newAge, and name match newName. I hope I explained clear enough, I'm not English speaking by nature, so, sorry if my English is a mess : )
+ 1
yes I thought of branching the if statement but I want user input to test the condition. let say from the code, I set age=18, name="Girly" and i want the user to guess the variable set when entering for age and name using the input function for both variables. How do I get users value against the set variable value and test with booleans either and, or or not.
+ 1
BINGO!!! that's it. yes
+ 1
can you explain a solution? I don't need the coding. let me try it out with a bit explanation.
+ 1
oh. OK. it is the conditioning aspect. how to decide and compare the user input with the set variables.
+ 1
wait. something just came to mind. actually was at work snicking to do some coding
+ 1
systemAge = 18
systemName = "Girly"
userInput_age = int(input("Guess Computer Age: "))
userInput_name = str(input("Guess Computer Name: "))
if not systemAge == userInput_age and systemName == userInput_name:
print("True but Inverted to False")
else:
print("No match for Logic")
+ 1
I suddenly realize I have not store the user input on a location and then used them to compare the variables.
and on the other hand, I guess the not operator is not needed for this code right? maybe will use and and or operators. multiple boolean is allowed on an expression right?