Python 3.x 简单Python 3.4.3
我是一个初学者(只写了14周的代码),我对这里发生的事情感到困惑。我只想问一个简单的问题,然后打印出另一个打印语句,但不管怎样,它总是回答“你说是的!”。有人请帮帮我Python 3.x 简单Python 3.4.3,python-3.x,Python 3.x,我是一个初学者(只写了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":
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.")
首先,要将输入存储在变量中:
string = input(...
然后,您必须对第二个或条件重复输入==“y”
:
if string == "Yes" or string == "y":
或
首先,要将输入存储在变量中:
string = input(...
然后,您必须对第二个或条件重复输入==“y”
:
if string == "Yes" or string == "y":
或
代码中存在多个问题
- 首先,从
input
方法获得的字符串不存储在任何地方。尝试打印输入
“变量”,您将获得:
<built-in function input>
然后,您的测试由两部分组成:第一个测试是正确的,但是第二个测试只是testif“y”
,这总是正确的,因为字符串“y”不是空的
您应将其替换为:
if input == "Yes" or input == "y":
或者更简单:
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.")
代码中存在多个问题
- 首先,从
input
方法获得的字符串不存储在任何地方。尝试打印输入
“变量”,您将获得:
<built-in function input>
然后,您的测试由两部分组成:第一个测试是正确的,但是第二个测试只是testif“y”
,这总是正确的,因为字符串“y”不是空的
您应将其替换为:
if input == "Yes" or input == "y":
或者更简单:
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.")
更详细地说,字符串“y”的真值为True,这就是if语句的计算结果始终为True的原因。“n”值也是如此,它也必须与输入进行比较。更详细地说,字符串“y”的真值为True,这就是if语句的计算结果始终为True的原因。“n”值也是如此,必须与输入进行比较。谢谢。我需要更多地了解输入、str-in和其他功能。谢谢。我需要更多地了解输入、str-in和其他功能。