以下是我正在尝试编写的脚本。我已经制作了其他工作脚本,包括简单的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")
答案 0 :(得分:3)
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)
sleep
模块中使用的time
函数。所以它会等待你想要的几秒钟,因为你现在看到它是2秒sleep(2)
您也可以使用format()
功能来放置;
print "I'm sorry to hear that {} ".format(name)
这是我最喜欢的功能,因为它非常实用,请检查here
答案 1 :(得分:2)
启动和运行的两件事
if answer = str("yes") :
这是错误的,它会指定一个字符串“是”来回答。因为它不会运行。使用
if answer == "yes" :
还要删掉
末尾不需要的引号print "I'm happy to hear that, " + str(name)"
代替
print "I'm happy to hear that, " + name
您应该只将字符串文字括在引号中,并且通常会有偶数引号。
最后一点是,如果值已经是字符串,则不需要所有这些str
次调用。它会像这样工作,但你正在做不必要的工作。
答案 2 :(得分:2)
以下是完整的完整脚本。当我不必像白痴一样时,我正在把输入串起来。我还有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")
答案 3 :(得分:0)
这是正确的版本:
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