Ask coding questions

← Back to all posts
How do I do this?????????
Kavin1983 (1)

i'm doing this code where the user inputs a string and I want to write a code where if the string the user typed string is not one of the words "title" "director" or "year" it would print "error", how would I do this?

Answered by RYANTADIPARTHI (6006) [earned 5 cycles]
View Answer
Comments
hotnewtop
RYANTADIPARTHI (6006)

Solution

arr = ['title', 'director', 'year']
user_inp = input("Type : ")
if not user_input in arr:
    print("Error!")
else:
    print("Correct!")

like that.
That should work

IcynDevz (744)

What lang are you using?

Kavin1983 (1)

thanks I got the answer but sorry for not including the language

Coder100 (16853)
text = input("type something: ")
if text not in ("title", "director", "year"):
  print("ERRROR!!!!!!!  aeouheorceaouhrceaodroclidrcoeuhildrocecrhuraeohirlcoehauoaeua")

ah a panicking program.

IMayBeMe (335)

I'm assuming your using python:

array_var = ["title", "director", 'year']
input = input('Enter something: ')
if input not in array_var:
  print('Error')
Bookie0 (5957)

What language?

Anyways, you'll first need an input to get something from the user. Then, use conditionals to determine if what the user is said is equal to those words. Then, if yes, output 'error'.

Try to do it first, and if you're stuck check this out (assuming it's Python):

user_input = input("Say something ") # the input

if user_input == "something": # conditionals
  print("Error") # output

And replace what you don't need with what you desire.

Lemme know if you have any questions! :D