+ 1
Input Question
So I'm kinda new to python, so I was running this test script, but when I try to run it, and I input "one" hit return, and then "two", I get: "type one two: A nope" I was expecting to get "A B" Why does it work for the first but not the second? Here's the code: test = input("type one two:") def test2(): if test == "one": print("A") if test == "two": print("B") else: print("Nope") test2()
1 Respuesta
+ 7
Firstly if statements aren't loop so when it matches a true condition that will be the output, so you input "one"
if test == "one":
print("A") #Match
Your whole code doesnt have elif statement so
if test == "one":
#You input one, match, the if statement below executed
if test == "two":
#You input one, and not two, so false, else statement executed
else:
print("Nope")
To make it work you do this
test = input()
if test == "one":
print("A")
elif test == "two":
print("B")
else:
print("Nope")
Input: one
Output: A
Input: two
Output: B
Input: C
Output: Nope