-3

For my code, I'm making an if statement typically an if statement that contains two if conditions. Look at the third line of code with hashtags beside it to understand what i'm asking

Look at the hashtags to understand my question. Parts where hashtags are beside are to either indicate that its related to my question or contains my question.

random1 = ["cool"]
random2 = ["wicked"]

import random
message = random.choice(random1 + random2) #######
print(message)
if message in random2:                     #######
    question = input("\ny/n ==> ")         #######
      if (question == "y"):
          print("hello")
      elif (question == "n"):
          print("what")

elif message in random1:
    print("lets go")

elif something in black and white in red:
    print("green")
    if (question == "n"): ###### (SINCE THE QUESTION CAN OCCUR OR NOT
                          ###### OCCUR, HOW DO I MAKE AN IF STATEMENT ALONG
                          ###### WITH THE STATEMENT WHEN THE USER SELECTS
                          ###### "N" For example "if (question == "n" and
                          ###### question does not exist)
        print("bye")
    if (question == "y"):
        print("pink")

Response to "This question may already have an answer here: Determine if variable is defined in Python"

My question is not a duplicate of the suggested post. I am not asking how to figure out if a variable is defined or not. I want to simply make a double if statement which includes one condition of when the user selects "n" and another condition of when the variable "question" does not exist, since "question" can randomly appear or not appear

John Row
  • 1
  • 3
  • SINCE "question" CAN OCCUR OR NOT OCCUR SINCE IT IS RANDOMLY CHOSEN, HOW DO I MAKE AN IF STATEMENT ALONG WITH THE STATEMENT WHEN THE USER SELECTS "N" For example "if (question == "n" and question does not exist – John Row Oct 09 '19 at 23:09
  • sorry for the caps, it's just to make my question easier to see – John Row Oct 09 '19 at 23:11
  • to clarify, "question" is one of the variables, it has nothing to do when I say "I need help with my question" – John Row Oct 09 '19 at 23:12
  • 3
    @JohnRow Instead of telling us to ignore blahblahblah, *remove it* and focus on your actual question. The caps don't make anything easier to read, either. – chepner Oct 09 '19 at 23:12
  • sorry i'm just kind of anxious because I thought I was finished with my code and all of a sudden it shows an error "question is not defined" since I set the variable "question" to random – John Row Oct 09 '19 at 23:15
  • Not sure if you know what I'm asking but basically I want to know how to make an if statement containing two of the following conditions in the SAME if statement: the user selects "n" AND the variable "question" does not exist, (because it was set as random.choice) – John Row Oct 09 '19 at 23:17
  • 1
    @Iguananaut If he's getting a "not defined" error, testing `if not question` won't fix that. – Barmar Oct 09 '19 at 23:18
  • 1
    The simplest solution is to assign a default value to the variable before the conditional assignments. – Barmar Oct 09 '19 at 23:19
  • Who can honestly tell? – Iguananaut Oct 09 '19 at 23:20
  • To me more clear, I made the variable "question" as a random choice, so it can appear or not appear. i want to put this in my if statement but am unsure how to. I want this to be included in the same if statement as (if question == "n") – John Row Oct 09 '19 at 23:20
  • So the way I want to do it is doing something like "if (question == "n" and question is not defined): – John Row Oct 09 '19 at 23:21
  • 1
    @JohnRow you have a fundamental misunderstanding, there is almost **never** any good reason to check if a variable exists in Python (this isn't Javascript, no siree). Instead of maybe or maybe not defining `question`, you should use a sentinel value and then check for that value. Your alternative is to use exception handling. – juanpa.arrivillaga Oct 09 '19 at 23:45

1 Answers1

0

Assign a default value to question before you assign it in the conditional code. This way, it will always be defined, even if the random code doesn't run.

question = "n"
if message in random2:
    question = input("\ny/n ==> ")
    if (question == "y"):
        print("hello")
    elif (question == "n"):
        print("what")
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Will it work if the "question" is not defined. Like for example, since "question" only appears randomly, will it still continue the code if the program randomly chose "question" not to appear? Basically will your provided code work something like this: if (question == "n" and question is not defined): – John Row Oct 09 '19 at 23:24
  • 2
    It's not random any more. The first line makes the variable always appear. – Barmar Oct 09 '19 at 23:25
  • You don't have to test if it's not defined, because it's always defined. – Barmar Oct 09 '19 at 23:25
  • 1
    hold on I am going to try it and tell you if it works – John Row Oct 09 '19 at 23:27
  • YES! adding "question = "n"" fixed everything. Thank you do much! By the way, the ordering of placing "question = "n"" matter? Like can I place it after the line "if message in random2:" or does it must be before it? – John Row Oct 09 '19 at 23:33
  • It has to be before it, that's the whole point. – Barmar Oct 09 '19 at 23:33
  • I'm a beginner in python so I have almost no experience with coding. And for my previous comment, I ask that because I think putting "question = "n"" after "if message in random2" will not affect anything, because we are just assigning a value to "question" and not actually telling the program to print anything yet – John Row Oct 09 '19 at 23:36
  • If you put it after the `if`, then it won't be done when the random choice isn't selected, which is the problem you're trying to solve. – Barmar Oct 09 '19 at 23:42
  • You already have the assignment after the `if`. – Barmar Oct 09 '19 at 23:42
  • oh I see now, its mainly because putting it before the if statement will give the variable "question" a value before actually having it randomly selected. So even if random choice isn't activated, the variable will still be defined since it was defined before the if statement – John Row Oct 09 '19 at 23:43
  • 1
    And the student was enlightened :) – Barmar Oct 09 '19 at 23:46