[英]why won't my python if else statement input HTML?
I am trying to make it so when the user types Y it creates an HTML file in an email.我试图这样做,所以当用户键入 Y 时,它会在 email 中创建一个 HTML 文件。 But every single time it get's to that part in the code it won't run the if else statement or the HTML file.
但是每次到达代码中的那一部分时,它都不会运行 if else 语句或 HTML 文件。
sendLetter = "let's send a letter to your boss and tell him how much you like your job y or n"
letterToBoss = """
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>dear Boss/h1>
<p>This is a paragraph.</p>
</body>
</html>
"""
if enjoyJob == "n" or "N":
print("that's so sad ".join(name))
input("why do you hate " + job + "?")
if enjoyJob == 'y' or 'Y':
print("that is awesome").join(name)
input(sendLetter)
if sendLetter() == 'y' or 'Y':
f = open("letter_to_Boss.html", "x")
f.write(letterToBoss)
f.read("letter_to_Boss.html")
f.close()
if sendLetter() == 'n' or 'N':
print("awe shucks")
enjoyJob = str(input("Do you enjoy your job?"))
or
statement. or
语句不能进行字符串比较。if enjoyJob == "n" or "N": # Does not work.
if enjoyJob in ("n", "N"): # Does work.
Python evaluates each side of the or
statement as its own. Python 将
or
语句的每一侧评估为自己的。 Your code is the equivalent of doing:您的代码相当于执行以下操作:
bool1 = bool(enjoyJob == "n") # Depends on enjoyJob.
bool2 = bool("N") # Is always true, since its not an empty string.
if bool1 or bool2:
....
name
and job
is not defined, .join()
does not do what you think it does. name
和job
没有定义, .join()
没有做你认为它做的事情。print("that's so sad ".join(name))
input("why do you hate " + job + "?")
>>> a = "hey"
>>> a.join("lol")
'lheyoheyl'
input(sendLetter)
does not create new variable sendLetter
. input(sendLetter)
不会创建新变量sendLetter
。 You must assign a variable to the input, and the parameter for the input
function is what is printed to the user.您必须为输入分配一个变量,
input
function 的参数是打印给用户的内容。 Correct usage is:正确用法是:
user_input = input("Please type in some input: ")
y
. y
你的逻辑必须做什么。 Notice:注意:
if sendLetter() == 'y' or 'Y':
f = open("letter_to_Boss.html", "x")
f.write(letterToBoss)
f.read("letter_to_Boss.html")
f.close()
If the user types n
, the program will crash since the file f
was never initialized.如果用户键入
n
,程序将崩溃,因为文件f
从未初始化。
f = open("letter_to_Boss.html", "x")
f.write(letterToBoss)
f.read("letter_to_Boss.html") # This will return an error.
f.close()
f.read()
will not allow you to read the file (you must open the file with the intention of reading it), and for your purposes, it has no use. f.read()
将不允许您读取文件(您必须打开文件以读取它),并且出于您的目的,它没有用。
With the corrections above, you get a code that looks more like so:通过上面的更正,您会得到一个看起来更像这样的代码:
letterToBoss = """<html>"""
name = str(input("What is your name?"))
job = str(input("What is your job?"))
enjoyJob = str(input("Do you enjoy your job?"))
if enjoyJob in ("n", "N"):
print(f"that's so sad {name}")
input(f"why do you hate {job}?")
elif enjoyJob in ("y", "Y"):
print(f"that is awesome {name}")
sendLetter = input("Would you like to send an email to your boss?")
if sendLetter == ("y", "Y"):
f = open("letter_to_Boss.html", "x")
f.write(letterToBoss)
f.close()
elif sendLetter == ("n", "N"):
print("awe shucks")
input()
returns a value. input()
返回一个值。 You don't call it then call the variable.if input(sendLetter) == 'y': # here comes the second problem
or
checks if two full expressions are true. or
检查两个完整表达式是否为真。 You can't use it like a == b or c
.a == b or c
那样使用它。 Or, exactly, you can use it, but if c is not None and not 0, it means True so the whole expression will be true.in
:in
:if input(sendLetter) in ('y', 'Y'):
# code here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.