Input in if/else statements

I am a new programmer, I don’t really know what I am doing that well, but I was sitting in class, and I thought to myself: hmm, can you put an input in an if-else statement?" and asked my professor, and he said that he has never thought about or tried doing this before. Is it possible that I am new and don’t know what I’m doing, or am I on to something here?

In principle you can use input in if else code.

Without a specific piece of code to review it is not possible to comment further.

1 Like

If you want to branch on user input:

if user_input := input("Do you want something? "):
    print(f"I'm sorry, I don't understand {user_input!r}.")

If you want to conditionally prompt the user for input:

val = 5

while val:
    val -= 1
    if not val:
        ret = input("How many more times do you want to do this? ")
        if ret.isdigit():
            val = int(ret)

Did you have something else in mind?


Maybe, this is what you have in mind?

response = ("Y", "N")

if input("(Y)es or (N)o: ").upper() in response:
    print("Not valid")