-1

I am trying to make it so when the user types Y it creates an HTML file in an email. 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.

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")

2 Answers2

3

1. You are missing your input code.

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

2. String comparisons cannot be done with the or statement.

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. 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.

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.

You must assign a variable to the input, and the parameter for the input function is what is printed to the user. 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.

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.

6. You do not (and can not) read from the opened file.

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.


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")
felipe
  • 7,324
  • 2
  • 28
  • 37
  • the name variable was added earlier – Zachary Blumstein Nov 02 '19 at 22:16
  • What do you mean? What # are you referring to? – felipe Nov 02 '19 at 22:17
  • Code is executed sequentially, which means if you do `print(name)` in one line, and then five lines later do `name = "Felipe"`, your program will crash saying it does not know what the variable `name` is on the `print(name)` line. – felipe Nov 02 '19 at 22:18
  • so python is different then javascript where in javascript I can store variables and in python, I can't – Zachary Blumstein Nov 02 '19 at 22:20
  • 1
    You can store variables in Python just the same way you can in Javascript, but with any programming language, you must do so sequentially for your machine to understand it. A computer is a "logic machine" for a lack of better words, and therefore you must give input to it in a way it can understand. In JS you do `var name = "Felipe"` before accessing it, in Python you have to do `name = "Felipe"` before accessing it as well. Compilers/interpreters aren't magical things that can guess what you mean -- you have to purposefully define variables before you access them. :) – felipe Nov 02 '19 at 22:23
  • I should also add that this wouldn't be the case if you were defining a function that takes in the parameter `name` and any other you are missing. – felipe Nov 02 '19 at 22:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/201770/discussion-between-zachary-blumstein-and-felipe-faria). – Zachary Blumstein Nov 02 '19 at 22:32
0
  1. input() returns a value. 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. You can't use it like 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. But instead, if you want to check for multiple values, you can use in:
if input(sendLetter) in ('y', 'Y'):
    # code here
CoderCharmander
  • 1,862
  • 10
  • 18
  • what is the `in` doing? – Zachary Blumstein Nov 02 '19 at 22:09
  • 1
    @ZacharyBlumstein If you do `if a == "str1" or "str2"` you are essentially doing `if a == "str1" or True` since Python evaluates each side of the `or` statement alone, and a non-empty string is always evaluated to be `True`. The `in` variable is literally asking Python `is var [in]side the list ('y', 'Y')`. – felipe Nov 02 '19 at 22:13
  • 1
    It checks if the value is in a collection. But check @FelipeFaria's answer, he described the problems better than me. – CoderCharmander Nov 02 '19 at 22:14