我正在为一个学校项目做这件事,需要知道为什么即使条件不满足,输出仍会不断吐出“爱沙尼亚”的声音。另外,为什么程序在某些地方没有显示“ None”? 我不确定是应该包含所有代码还是仅包含部分代码,因此我将整件事放在以防万一。我是新来的,所以请保持我的学识...
# Purpose: Create a program that suggests a vacation spot based on user input
# Here is the Input, or answers to the questions
print('Hello! This is your Dream Vacation Spot!')
answers = [input('Do you want a "hot" or "cold" place? '),
input('Do you want a place that has a "huge" pop or "small" pop? '),
input('Are you planning on getting wet? "y" or "n": ')]
# Create the intro that displays what they said.
def intro():
for i in range(2):
print('')
print('Hi! Welcome to your Dream Vacation Spot!')
print('')
print('Listed here, are your answers:')
print('Hot or cold place? You said ' + answers[0])
print('Huge population, or small towny place? You said ' + answers[1])
print('Want to get wet? You said ' + answers[2])
for i in range(2):
print('')
# Start using the if/ elif statements to decide places to go.
def main():
if answers[0] == 'cold' and answers[1] == 'small' and answers[2] == 'wet' or 'Wet':
one = 'Congrats!! You got... \n Estonia!'
print(one)
elif answers[0] == 'cold' or 'Cold' and answers[1] == 'big' or 'Big' and answers[2] == 'y' or 'Y':
two = 'Congrats!! You got... \n Geirangerfjord, Norway!'
print(two)
elif answers[0] == 'cold' or 'Cold' and answers[1] == 'small' or 'Small' and answers[2] == 'n' or 'N':
three = 'Congrats!! You got... \n Innsbruck, Austria!'
print(three)
elif answers[0] == 'cold' or 'Cold' and answers[1] == 'big' or 'Big' and answers[2] == 'n' or 'N':
four = 'Congrats!! You got... \n Yellowknife, Canada!'
print(four)
elif answers[0] == 'hot' or 'Hot' and answers[1] == 'big' or 'Big' and answers[2] == 'n' or 'N':
five = 'Congrats!! You got... \n Austin, Texas!'
print(five)
elif answers[0] == 'hot' or 'Hot' and answers[1] == 'small' or 'Small' and answers[2] == 'n' or 'N':
six = 'Congrats!! You got... \n Bisbee, Arizona!'
print(six)
elif answers[0] == 'hot' or 'Hot' and answers[1] == 'small' or 'Small' and answers[2] == 'n' or 'N':
seven = 'Congrats!! You got... \n Grand Junction, Colorado!'
print(seven)
elif answers[0] == 'hot' or 'Hot' and answers[1] == 'small' or 'Small' and answers[2] == 'y' or 'Y':
eight = 'Congrats!! You got... \n Muskogee, Oklahoma!'
print(eight)
def printAll():
print(intro())
print(main())
for i in range(2):
print('')
print('Have a nice trip!!!')
printAll()
答案 0 :(得分:1)
Ph!那是很多代码,关于如何处理复杂的逻辑,可能有很多很棒的课程,让我们看看我们是否不能仅仅使其变得更好一点。
首先,我建议您使用列表理解和lower()函数来简化答案中的可能性
answers_lowercase = [x.lower() for x in answers]
然后为什么不在if / elif语句中使用单个逻辑测试,例如
def main():
if answers_lowercase == ['cold', 'small', 'wet'] :
print('Congrats!! You got... \n Estonia!')
可能还有更简洁的Python方式来编写代码:即从表中查找正确的答案,而不是使用六个“ if”语句;如果用户在输入中输入了错误的内容,则会进行错误处理。值得研究和学习的好东西。祝你好运!
答案 1 :(得分:0)
如果需要,可以更改条件以使用“ in”关键字,以避免“ and”,“ or”之间的混淆 例如。 (“冷”,“冷”)中的答案[0]和(“大”,“大”)中的答案[1]和(“ y”,“ Y”)中的答案[2]
答案 2 :(得分:-1)
您的问题在于if语句。当您检查or
条件时,请用方括号将其包围。
例如answers[0] == ('cold' or 'Cold') and answers[1] == ('big' or 'Big') and answers[2] == ('y' or 'Y')