0

How do I get a specific input in python such as

variable = input() 
if variable == "Specific input":
    do stuff

I need the input to be one of two options say X or O

kfman18
  • 13
  • 1
  • 6
  • Can you be more specific about your question ? If you want to perform some operation only when the input is x or o you need to check for them. – Vinayak Kolagi Jun 15 '16 at 04:48
  • What is wrong with your code? It should work just fine. You can't force a user to give you specific input. You can use a `while` loop to check if `variable` is one of `x` and `o`, else keep prompting for correct input. – SvbZ3r0 Jun 15 '16 at 04:48
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – moooeeeep Oct 08 '19 at 15:05

4 Answers4

1

Its Simple Example to use specific Variable: This is Simple Code For Converting Between Inch-to-Cm OR Cm-to-Inch:

conv = input("Please Type cm or inch?")
if conv == "cm" :
    number = int(input("Please Type Your number: "))
    inch = number*0.39370
    print(inch)
elif conv == "inch":
    number = int(input("Please Type Your Number?"))
    cm = number/0.39370
    print(cm)
else:
    print("Wrong Turn!!")
CU12
  • 54
  • 7
ALiReza Ch
  • 36
  • 3
0

Define a list

specific_input = [0, 'x']

and so

if variable in specific_input:
    do awesome_stuff
elm
  • 20,117
  • 14
  • 67
  • 113
0

I understand the question more on looping for relevant input (as @SvbZ3r0 has pointed out in comment). For a very beginner in Python it may be hard to even copy code from tutorials, so:

#! /usr/bin/env python
from __future__ import print_function

# HACK A DID ACK to run unchanged under Python v2 and v3+:
from platform import python_version_tuple as p_v_t
__py_version_3_plus = False if int(p_v_t()[0]) < 3 else True
v23_input = input if __py_version_3_plus else raw_input

valid_ins = ('yes', 'no')
prompt = "One of ({0})> ".format(', '.join(valid_ins))
got = None
try:
    while True:
        got = v23_input(prompt).strip()
        if got in valid_ins:
            break
        else:
            got = None
except KeyboardInterrupt:
    print()

if got is not None:
    print("Received {0}".format(got))

Should work on either python v2 and v3 unchanged (there was no indication in question and input() in Python v2 might not be a good idea ...

Running the above code in Python 3.5.1:

$ python3 so_x_input_loop.py 
One of (yes, no)> why
One of (yes, no)> yes
Received yes

Breaking out via Control-C:

$ python3 so_x_input_loop.py 
One of (yes, no)> ^C

When it is clear, on which version to run, say python v3, than the code could be as simple as:

#! /usr/bin/env python

valid_ins = ('yes', 'no')
prompt = "One of ({0})> ".format(', '.join(valid_ins))
got = None
try:
    while True:
        got = input(prompt).strip()
        if got in valid_ins:
            break
        else:
            got = None
except KeyboardInterrupt:
    print()

if got is not None:
    print("Received {0}".format(got))
Dilettant
  • 3,267
  • 3
  • 29
  • 29
0

The simplest solution would be:

variable = input("Enter x or y") acceptedVals = ['x','y'] if variable in acceptedVals: do stuff

Or if you want to keep prompting the user for the right use a while loop variable = None acceptedVals = ['x','y'] while variable not in acceptedVals variable = input("Enter x or y") do stuff

Sreejith Menon
  • 1,057
  • 1
  • 18
  • 27
  • 1
    Your second option will require you to define `variable` before your `while` statement or else you will get an error that says: `UnboundLocalError: local variable 'variable' referenced before assignment`, FYI. But it does work. – q-compute Aug 22 '19 at 21:08