Python 3:random.带有 if 语句的选择命令
Python 3:random.choice command with if statments
我是 python/programing 的新手,而且我一直在玩一个小型随机选择生成器。到目前为止,我对一切的工作方式感到非常满意,但我对 if 语句有疑问。我该如何发出信号,表明 if 应该指向 table Desert 中第一个生成的选择,而不是生成第二个选择?我试过在 print 命令中嵌套 if 语句,但出现了很多语法错误。这是代码:
import random
import sys
Name = ['Dave', 'Suzane', 'Jim', 'Diana']
Appitizers = ['Spinach & Artichoke Dip', 'Crispy Green Beans', 'Half Chicken Quesadila', 'None, for me, just the main course please']
MainCourse = ['Steak', 'Sea Food', 'Grilled Chicken', 'House Salad', 'Rib Sanwich', 'Soup of the Day']
Desert = ['Pie of the Day', 'Chocolate Moose', 'Cheeze Cake', "No Thanks, I'm Full!"]
Goodbye = ['Captian', 'Monsiure', 'Sir', 'Madame',]
Goodbye2 = ['Come back again!', 'See you soon', 'Thank you for stopping by', 'Thank you for choosing us!']
print("Hello ", random.choice(Name), "tonight you're meal will be:")
print("Appitizer:", random.choice(Appitizers))
print("Followed by Main Coure:", random.choice(MainCourse))
print("And finally Desert:", random.choice(Desert))
if random.choice(Desert)=="No Thanks, I'm Full!": print('Farwell!', random.choice(Goodbye1)), sys.exit()
print(random.choice(Goodbye2))
谢谢!
将random.choice(Desert)
的结果分配给一个变量,并在适用的地方使用它。 (当你这样做时,将 Goodbye1
更改为存在的变量,例如 Goodbye
)
x = random.choice(Desert)
print("And finally Desert:", x)
if x=="No Thanks, I'm Full!":
print('Farwell!', random.choice(Goodbye))
sys.exit()
我是 python/programing 的新手,而且我一直在玩一个小型随机选择生成器。到目前为止,我对一切的工作方式感到非常满意,但我对 if 语句有疑问。我该如何发出信号,表明 if 应该指向 table Desert 中第一个生成的选择,而不是生成第二个选择?我试过在 print 命令中嵌套 if 语句,但出现了很多语法错误。这是代码:
import random
import sys
Name = ['Dave', 'Suzane', 'Jim', 'Diana']
Appitizers = ['Spinach & Artichoke Dip', 'Crispy Green Beans', 'Half Chicken Quesadila', 'None, for me, just the main course please']
MainCourse = ['Steak', 'Sea Food', 'Grilled Chicken', 'House Salad', 'Rib Sanwich', 'Soup of the Day']
Desert = ['Pie of the Day', 'Chocolate Moose', 'Cheeze Cake', "No Thanks, I'm Full!"]
Goodbye = ['Captian', 'Monsiure', 'Sir', 'Madame',]
Goodbye2 = ['Come back again!', 'See you soon', 'Thank you for stopping by', 'Thank you for choosing us!']
print("Hello ", random.choice(Name), "tonight you're meal will be:")
print("Appitizer:", random.choice(Appitizers))
print("Followed by Main Coure:", random.choice(MainCourse))
print("And finally Desert:", random.choice(Desert))
if random.choice(Desert)=="No Thanks, I'm Full!": print('Farwell!', random.choice(Goodbye1)), sys.exit()
print(random.choice(Goodbye2))
谢谢!
将random.choice(Desert)
的结果分配给一个变量,并在适用的地方使用它。 (当你这样做时,将 Goodbye1
更改为存在的变量,例如 Goodbye
)
x = random.choice(Desert)
print("And finally Desert:", x)
if x=="No Thanks, I'm Full!":
print('Farwell!', random.choice(Goodbye))
sys.exit()