Using of the value of an input() element without using a variable

Hello everyone. I have a problem with my code. I would like to directly display the value of an input() without using a variable

myList = ["Apple", "Banana", "Orange", "Lemon", Pineapple]

if input() in phone_book:
    print(input())
else:
    print("Not found")

In that case, the input() execute twice, instead of once.
I’d like to print the value of the input() when this value is part of the list. But I don’t want to use a variable to do that.
How can we do that?

Hi,

This would be a highly unusual way of structuring things.
While, the first check would work, when you run input() the second time, it will just prompt you to input something again. So you can check for membership once but then that string is thrown away.

A reason why it’s impractical is that the input() value is always going to be a string. Sure you could parse it in the line but that could start getting really messy.

Example:

#!/usr/bin/env python3

some_list = [1,2,3,4,5]

if input("Enter an integer: ") in some_list:
    print("the number was in the list!")
else:
    print("Not found")

$ ./test.py
Enter an integer: 3
Not found

We’d need at least if int(input("Enter an integer: ")) in some_list:… but some inputs may require parsing so it’s limited and arguably harder to read.

4 Likes

I see. So the only way to get this value is only by using a variable?

I mean it will run with the adjustment to parse… it’s just a bit clunky in my opinion. :slight_smile:

This would work:

#!/usr/bin/env python3

some_list = [1,2,3,4,5]

if int(input("Enter an integer: ")) in some_list:
    print("the number was in the list!")
else:
    print("Not found")