我正在从事硬件作业,在那里我创建了一个带有入门问题的假俱乐部。如果以“否”回答任何问题,则不允许该人加入。
我尝试过回过头来学习关于列表和循环的课程,但是我找不到在那做的事情。
到目前为止,这是我的代码。
# Purpose: Create a fake club that has certain requirements, ask user to
# fill out the application, and print out their answers + results.
def main():
display = input('Hi! This is your application to The Aqua Project...')
display2 = input('Read the following questions and just type y or n')
# Weird list format...
user = [input('Are you at least 18 yrs old? '),
input('Can you work with other people? '),
input('Do you like animals? '),
input('Are you okay with getting dirty sometimes? ')]
# Here's the problem, I want to print 'sorry you cant join' once...
for i in range(4):
if user[i] != 'y':
print('Sorry, but you can\'t join our club')
justToShowInCMD = input('')
i += 1
else:
print('')
print('Congratulations, you have met all of our requirements!')
print('We will send an email soon to discuss when our team')
print('will meet up to help save some animals!')
print('In the meantime, visit our website at
TheAquaProject.com')
justToShowInCMD = input('')
main()
如果您对某些问题输入“ n”,则表示可以加入,对于其他问题,则表示您不能加入。我不知道为什么有时候在面试中说“不”时它会说可以。
答案 0 :(得分:2)
通常的方法是使用StateID
和for
子句的break
循环:
else
或for answer in user:
if answer != 'y':
print('Sorry')
break
else:
print('Congratulations')
函数:
any()
答案 1 :(得分:0)
如果一个“否”表示拒绝,则可以添加break
以在打印拒绝信息后退出循环。就像:
for i in range(4):
if user[i] != 'y':
print('Sorry, but you can\'t join our club')
justToShowInCMD = input('')
# i += 1 # <<<<<<<<<<<<<<<<<< this code may be not needed here
break # <<<<<<<<<<<<<<<<<< where to add break
else:
print('')
print('Congratulations, you have met all of our requirements!')
print('We will send an email soon to discuss when our team')
print('will meet up to help save some animals!')
print('In the meantime, visit our website at
TheAquaProject.com')
justToShowInCMD = input('')
或者您可以使用变量来表示是否拒绝,就像:
toDecline = False
for i in range(4):
if user[i] != 'y':
toDecline = True
if toDecline:
print('Sorry, but you can\'t join our club')
justToShowInCMD = input('')
else:
print('')
print('Congratulations, you have met all of our requirements!')
print('We will send an email soon to discuss when our team')
print('will meet up to help save some animals!')
print('In the meantime, visit our website at
TheAquaProject.com')
justToShowInCMD = input('')
答案 2 :(得分:0)
有几种方法可以做到这一点:
no
,效率会很低)no
响应后立即退出。 (可能会引起混淆)any
方法。 (可能会造成混淆,不建议使用)flag = True
for i in range(4):
if user[i] != 'y':
flag = False # User has answered no to something, set the flag to false
if flag: # User has answered yes to everything
# <do your `yes` output>
else: # User has answered no to something
# <do your `no` output>
答案 3 :(得分:0)
您的代码中有一些小地方需要更改:
-w libcxx
答案 4 :(得分:0)
main()
的评论:input
和print
)。user
列表上,只需反复调用input
user
,它实际上将成为y
和或n
值的列表,然后您可以通过循环将其解包以检查这些值。user
列表方法的另一个问题是,在取消资格之前,需要回答所有问题。如果有40个问题怎么办?我会很生气。for value in user:
。无需使用python通过索引来寻址列表。main()
实现:def print_list(values: list):
"""Print the values of a list"""
for value in values:
print(value)
def main():
"""Iterate through a questionnaire"""
# Variables at the top
intro = ['Hi! This is your application to The Aqua Project...',
'Read the following questions and just type y or n']
questions = ['Are you at least 18 yrs old? ',
'Can you work with other people? ',
'Do you like animals? ',
'Are you okay with getting dirty sometimes? ']
final = ['\nCongratulations, you have met all of our requirements!',
'We will send an email soon to discuss when our team',
'will meet up to help save some animals!',
'In the meantime, visit our website at www.TheAquaProject.com']
print_list(intro)
for i, question in enumerate(questions, start=1):
response = input(question)
if response == 'n': # can be replaced with != 'y'
print("Sorry, you can't join the club!")
break
if i == len(questions):
print_list(final)
main()
的评论:print
函数以进行打印,而不是多次调用print_list
。
values: list
这是一个type
提示,它告诉data type
的{{1}}参数应该是values
。print_list
:使用文档字符串
""""""
:Defining Main Functions in Python main()
很明显,它只是questions
中的问题,作为user
,而没有调用list
input
打开questions
的包装,然后使用built-in function: enumerate
。
for-loop
和i
一起计数。enumerate
,然后使用count = 0
来跟踪循环迭代;这被视为不是pythonic 。 Python使用count+=1
,但前提是您需要计数才能实现其他功能。enumerate
条件检查if
是否为response
n
条件的评估结果为if
(例如,当True
,response = n
为n == n
时),用户将得到对不起 strong>消息True
,结束循环,问卷已完成。break
,而只会检查y
。那不在问题的范围内。n
的{{1}}参数设置为1。如果回答了所有问题,则start
和enumerate
使得i=4
的评估结果为{{1 }},用户将收到祝贺消息。
len(questions)=4
而不是i == len(questions)
,因为将这样的值硬编码是不好的主意,因为那样的话您必须记住自己已经这样做了。如果问题数量发生变化怎么办?然后您的True
条件被破坏了。答案 5 :(得分:0)
我建议将您的问题存储在列表中,并使用for循环提问。将用户的响应存储到另一个列表中,并检查此列表中是否有“ n”。参见下面的代码:
Dim strSheetTemp As String
strSheetTemp = outsheet + "-temp"
wsCopy.Copy Destination:=ThisWorkbook.Worksheets(strSheetTemp)
答案 6 :(得分:0)
假设您基于range(4)
的长度迭代4次(使用user
),则可以简单地执行以下操作:
if 'n' or 'no' in user:
print('Sorry, but you can\'t join our club')
justToShowInCMD = input('')
else:
print('')
print('Congratulations, you have met all of our requirements!')
print('We will send an email soon to discuss when our team')
print('will meet up to help save some animals!')
print('In the meantime, visit our website at
TheAquaProject.com')
justToShowInCMD = input('')
您可以修改if
条件以迎合其他形式的否定答案,例如'N' or 'No'
。您无需遍历user
。