Skip to main content
 首页 » 编程设计

Python2字输入if语句脚本

2025年05月04日15sharpest

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

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

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 已经返回一个字符串,因此您不必再次将其转换为字符串。不需要 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()函数来放置like;

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

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