简体   繁体   English

为什么我的 python if else 语句不能输入 HTML?

[英]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")

1. You are missing your input code. 1.您缺少输入代码。

enjoyJob = str(input("Do you enjoy your job?"))

2. String comparisons cannot be done with the or statement. 2. 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:
    ....

3. name and job is not defined, .join() does not do what you think it does. 3. namejob没有定义, .join()没有做你认为它做的事情。

print("that's so sad ".join(name))
input("why do you hate " + job + "?")
>>> a = "hey"
>>> a.join("lol")
'lheyoheyl'

4. input(sendLetter) does not create new variable sendLetter . 4. 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: ")

5. You must state what your logic must do if the user doesn't specify y . 5. 你必须 state 如果用户没有指定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从未初始化。

6. You do not (and can not) read from the opened file. 6.您不(也不能)从打开的文件中读取。

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()将不允许您读取文件(您必须打开文件以读取它),并且出于您的目的,它没有用。


Final Code最终代码

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")
  1. input() returns a value. input()返回一个值。 You don't call it then call the variable.你不调用它然后调用变量。 So, this would be correct:所以,这将是正确的:
if input(sendLetter) == 'y': # here comes the second problem
  1. 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.或者,确切地说,您可以使用它,但如果 c 不是 None 也不是 0,则表示 True,因此整个表达式将为 true。 But instead, if you want to check for multiple values, you can use in :但是,如果要检查多个值,则可以使用in
if input(sendLetter) in ('y', 'Y'):
    # code here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM