我想根据您首先选择的答案打印声明以进行更改。而且您只有一定的机会。
例如,您有三个选择A,B和C。如果先选择A,它会说:“那是错误的”;如果您选择B是第二个,它会说“那仍然是错的,如果您选择C” “你还在尝试吗?。
但是我首先选择B或C首先会说“那是错误的”。根据您选择的内容,打印的第一,第二或第三位会改变
for tries in range(3):
Response = input("""Response
A)Love
B)Dog
C)Cast
D)Blowerfish""")
if Response != "A" or Response != "a":
print("That is wrong")
break
elif: Response != "A" or Response != "a"
print("That is still wrong")
elif: Response != "A" or Response != "a"
print("Are you even trying")
else:
print("Out of Chances")
我怎样才能使其正常工作,或者我必须对其进行添加或更改以使其按自己的方式工作。
答案 0 :(得分:1)
您可以在if-else逻辑中使用tries
。
if Response != "A" or Response != "a":
if tries == 0:
print("That is wrong")
elif tries == 1:
print("That is still wrong")
elif tries == 2:
print("Are you even trying")
答案 1 :(得分:0)
使用当前迭代的索引(尝试)来确定要显示的错误消息。可以使用笨拙的if语句来完成此操作,或者您几乎可以从列表中检索消息-更好。
prompt = 'a, b, or c: ' #your multi-line input string
err_messages = ['That is wrong', 'That is still wrong','Are you even trying?']
for tries in range (3):
response = input(prompt)
if response.lower() == 'd':
print('you got it right')
break
elif response.lower() in 'abc':
print(err_messages[tries])
else:
print('invalid choice')
我也将大写变量重命名为小写,因为大写通常保留用于类名
答案 2 :(得分:0)
我不确定,如果我正确理解了您的问题,但是如果您尝试使每次尝试的信息都不相同,则可以执行以下操作:
try_counter = 0 # Initialize try_counter
while try_counter < 3: # Ask until user is out of chances
Response = input('...')
if Response not in "Aa": # if Response isn't 'A' or 'a'
print((
'That is wrong',
'That is still wrong',
'Are you even trying'
)[try_counter]) # Create a tuple/list of messages and select the one on index of try_counter, so when try_counter is 0, it will select the fist message etc.
try_counter += 1 # Move to next chance.
else:
print('That is correct! ')
break # We don't want to ask again if the answer is correct
if not try_counter < 3: # This will be executed only if the cycle was left because of the given rule.
print('Out of Chances')
Python并不关心是否已经执行过if块,因此我一直在使用第一种情况。您可以代替尝试计数,并通过尝试计数器选择消息并打印消息。
答案 3 :(得分:0)
您应该阅读有关if语句和循环的更多信息。您当前的实现通过相同的if语句循环三次。当前,如果您的响应是a,则循环始终在第一个if语句或其他语句直接停止,因为您将同一事物进行了三次比较。除非您想尽早退出循环,否则应删除break语句。解决此问题的简单方法是将try变量添加到所有if语句中,以便除了响应之外还比较try的数量,例如if(Response!=“ A”或Response!=“ a”)和try == 0:这不是实现此目标的好方法,但这应该可以帮助您入门。
在elif语句的错误位置上也有:应该位于行的末尾。
答案 4 :(得分:0)
首先,语法elif:
是错误的,您应该在末尾使用:
。
示例:elif Response != "A" or Response != "a":
另一方面,要实现您所要求的功能,您需要保存第一个输入并检查较新的输入,然后在诸如elif response is not "A" for 2 times or more