Python2字输入if语句脚本

标签 python if-statement words

下面是我正在尝试编写的脚本。我已经制作了其他工作脚本,包括简单的 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

关于Python2字输入if语句脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28248829/

相关文章:

mysql - 表中的 SQL 字数限制

PHP : Find repeated words with and without space in text

python - 保存方法中的django回滚事务

python - 从多维 Numpy 数组行中选择随机窗口

python - "TypeError: ' 诠释 ' object does not support item assignment";迭代问题

java - 使用循环和 if 语句从方法返回时出错

java - System.out.println 行影响逻辑的执行

python如何改变全局变量

c++ - 仅在 vector 末尾打印新行 - "if"循环之外的 "for"语句

c - C 中的唯一字计数器