对于我的代码,我正在做一个if
语句,通常是一个包含{em>两个 if
条件的if
语句。查看代码第三行,并在其旁边加上标签以了解我的要求
查看主题标签以了解我的问题。主题标签旁边的部分要么表明其与我的问题有关,要么包含我的问题。
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")
回答“此问题可能已经在这里得到答案: 确定是否在Python中定义了变量”
我的问题不是建议的帖子的重复。我不是在问如何弄清楚是否定义了变量。我只想简单地做一个double if语句,它包含一个条件,当用户选择“ n”时,另一个条件,当变量“ question”不存在时,因为“ question”可以随机出现或不出现
答案 0 :(得分:0)
在条件代码中将默认值分配给question
之前。这样,即使没有运行随机代码,也将始终对其进行定义。
question = "n"
if message in random2:
question = input("\ny/n ==> ")
if (question == "y"):
print("hello")
elif (question == "n"):
print("what")