我是初学者(仅编码14周),我对此处发生的事情感到困惑。我想做的就是问一个简单的问题并打印出另一份印刷声明,但无论如何,它总是回答"你说是的!"。有人请帮帮我。
input("This Python program will ask you a series of questions. Are you Ready? ")
if input == "Yes" or "y":
print("You said yes!")
elif input == "No" or "n":
print("You said no!")
else:
print("You said neither.")
答案 0 :(得分:3)
您的代码中存在多个问题。
首先,您从input
方法获得的字符串不会存储在任何位置。尝试打印<built-in function input>
&#34;变量&#34;,您将获得:
input
相反,将input
方法的输出存储在变量中,并使用此变量而不是if input == "Yes" or "y":
。
第二个问题,你的测试。当你写if (input == "Yes") or ("y"):
时,我想你想测试字符串是否等于&#34;是&#34;或者&#34; y&#34;。但实际上,可以写出测试结果:
test if "y"
您的测试由两部分组成:第一个测试是正确的,但第二个测试只是if input == "Yes" or input == "y":
,因为字符串&#34; y&#34;不是空的。
您应该将其替换为:
if input in ("Yes", "y"):
甚至更简单:
str = input("This Python program will ask you a series of questions. Are you Ready? ")
if str in ("Yes","y"):
print("You said yes!")
elif str in ("No","n"):
print("You said no!")
else:
print("You said neither.")
总而言之,最终的代码很简单:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ThisSheet As Worksheet
Dim NewRow As Long
Dim OldTotalRange As Range
Dim OldTotalRow As Long
Set ThisSheet = ActiveSheet
'If not single cell is changed, exit sub
If Target.Cells.Count = 1 Then
'Disable events for prevent recursion
Application.EnableEvents = False
If Target.Column = 1 And Target.Row <> 1 And Target.value <> "" Then
If IsDate(Target.value) And IsDate(Target.Offset(-1, 0).value) Then
If Month(Target.value) <> Month(Target.Offset(-1, 0).value) Then
With ThisSheet
NewRow = Target.Row
On Error Resume Next
Set OldTotalRange = .Columns(1).Find(What:="Total", After:=Target, SearchDirection:=xlPrevious)
OldTotalRow = OldTotalRange.Row
'It's for first 'Total' when there isn't 'totals' before.
If OldTotalRow = 0 Then
OldTotalRow = 1
End If
.Rows(NewRow).Insert
.Cells(NewRow, 1) = "Total"
.Cells(NewRow, 4).FormulaR1C1 = "=SUM(R[-" & NewRow - OldTotalRow - 1 & "]C:R[-1]C)"
.Cells(NewRow, 5).FormulaR1C1 = "=SUM(R[-" & NewRow - OldTotalRow - 1 & "]C:R[-1]C)"
'It's formatting, you can delete it or change
.Range(.Cells(NewRow, 1), .Cells(NewRow, 5)).Interior.Color = RGB(196, 215, 155)
.Range(.Cells(NewRow, 1), .Cells(NewRow, 5)).Font.Bold = True
End With
End If
End If
End If
Else
Exit Sub
End If
'Enable events
Application.EnableEvents = True
End Sub
答案 1 :(得分:1)
首先,您希望将输入存储在变量中:
string = input(...
然后,您必须为input == "y"
条件中的第二个重复or
:
if string == "Yes" or string == "y":
或
if string in ("Yes", "y"):