Python2 单词输入 if 语句脚本

Python2 word input if-statement script

下面是我正在尝试编写的脚本。我已经制作了其他工作脚本,包括简单的 if 语句脚本,但我想尝试一下并尝试制作一个依赖于字符串输入而不是整数或浮点数的脚本。

不用说,我仍在学习,不会要求它来完成我的工作,但如果您决定尝试一下,感谢您的尝试。我花了大约一个小时和另一个编码新手一起尝试微小的调整。我几乎可以肯定,让输入等于一个字符串开始是徒劳的,造成我的冲突。

answer = raw_input("Do you enjoy your work?\n")

print str(answer)
if answer = str("yes") :
    print "I'm happy to hear that, " + str(name)"
    print "I wonder what being a " + str(title) + " actually means."
    print "I don't have the term in my vocabulary.  I'm a machine."
    raw_input("My script is over soon.  Goodbye.\n")
else :
    print "I'm sorry to hear that, " + str(name)"
    print "I guess being a " + str(title) + " must be difficult."
    raw_input("My script is over soon.  Goodbye.\n")

raw_input returns 已经是一个字符串,因此您不必再次将其转换为字符串。不需要str("yes")这个。

另外你的意图是错误的。一定是;

if answer == "yes":

你的整个脚本一定是这样的;

from time import sleep
answer = raw_input("Do you enjoy your work?\n")

print answer
if answer == "yes":
    print "I'm happy to hear that, " + name)"
    print "I wonder what being a " + title + " actually means."
    print "I don't have the term in my vocabulary.  I'm a machine."
    print ("My script is over soon.  Goodbye.\n")
    sleep(2)
else :
    print "I'm sorry to hear that, " + name"
    print "I guess being a " + title + " must be difficult."
    print ("My script is over soon.  Goodbye.\n")
    sleep(2)

使用了 time 模块中的 sleep 函数。所以它会等待你想要的秒数,因为你现在看到它是 2 秒 sleep(2)

也可以使用format()功能点赞;

print "I'm sorry to hear that {} ".format(name)

这是我最喜欢的功能,因为它非常有用,检查它here

这是正确的版本:

answer = raw_input("Do you enjoy your work?\n")

print answer
if answer == "yes" : # use == to judge string equals but not =
    print "I'm happy to hear that, " + str(name)
    print "I wonder what being a " + str(title) + " actually means."
    print "I don't have the term in my vocabulary.  I'm a machine."
    raw_input("My script is over soon.  Goodbye.\n")
else :
    print "I'm sorry to hear that, " + str(name)
    print "I guess being a " + str(title) + " must be difficult."
    raw_input("My script is over soon.  Goodbye.\n")

在你的代码中你的if是错误的,你应该使用==来代替=。 代码如下:

ans = raw_input("input word:")
if ans = 'yes':
  print 'yes'
else:
  print 'no'

SyntaxError: invalid syntax

2 件事让你起床 运行宁

if answer = str("yes") :

错了这会分配一个字符串"yes"来回答。事实上它不会 运行。使用

if answer == "yes" :

同时去掉

末尾不需要的引号
print "I'm happy to hear that, " + str(name)"

改为

print "I'm happy to hear that, " + name

您应该只用引号将字符串文字括起来,并且通常会有偶数个引号。

最后一点是,如果值已经是字符串,则不需要所有这些 str 调用。它会像这样工作,但你在做不必要的工作。

这是完整的脚本。我在不必要的时候串起输入,就像一个白痴。我还有 3 个错别字需要更正。

print "Hello. I'm a program made to ask generic questions and reply."
print "I'm not advanced enough to react to questions I'm asked."
print "If I had to pick a name for myself, it would be Dan."
name = raw_input("What is your name?\n")

title = raw_input("You already know what I do for work.  What is your official title?\n")

answer = raw_input("Do you enjoy your work?\n")

if answer == str("yes") :
    print "I'm happy to hear that, " + name
    print "I wonder what being a " + title + " actually means."
    print "I don't have the term in my vocabulary.  I'm a machine."
    raw_input("My script is over soon.  Goodbye.\n")
else :
    print "I'm sorry to hear that, " + name
    print "I guess being a " + title + " must be difficult."
    raw_input("My script is over soon.  Goodbye.\n")