1

I am trying to add variable information from an input to a text document. The document is in the place it should be. I have this code so far:

import time
import os
print("Welcome!")
name = input("Please enter your name: ")
print("Hello",name, "! I am going to guess your most favorite type of music.")
time.sleep(2)
print("Please, choose from one of the following: ")
listening_time = ["1 - One hour a day", "2 - About two hours per day", "3 - three to four hours per day", "4 - Most of the day"]
print(listening_time)
how_often = int(input("I find myself listening to music..."))

def add_file_1(new_1):
    f = open("music.txt", "a")
    f.write("1 Hour")

def add_file_2(new_2):
    f = open("music.txt", "a")
    f.write("2 Hours")

def add_file_3(new_3):
    f = open("music.txt", "a")
    f.write("3 - 4 Hours")

def add_file_4(new_4):
    f = open("music.txt", "a")
    f.write("Most of the day")

if how_often == str('1'):
    add_file_1(new_1)
elif how_often == str('2'):
    add_file_2(new_2)
elif how_often == str('3'):
    add_file_3(new_3)
else:
    add_file_4(new_4)
Jason White
  • 39
  • 1
  • 1
  • 7
  • What is your question in specific then? – blhsing Jul 11 '18 at 00:51
  • 8
    It looks like you define `how_often` as an int, then test its equality to strings – Brad Solomon Jul 11 '18 at 00:52
  • I tried changing it to be a string to a string comparison and I get an error. Before (with the code above) I would get no error. It just wouldn’t do anything to the text file. – Jason White Jul 11 '18 at 01:03
  • The question is: how can I change the code to do something. I get no error the way it is. It just doesn’t do anything to the text file. My last version of the code I tried putting `f = open("music.txt", "a") f.write(“2 Hours”)` inside of the if statement itself and got no result. – Jason White Jul 11 '18 at 01:05
  • What is `new_1`? – Mateen Ulhaq Jul 11 '18 at 01:06
  • Consider `f.close()`. Better, use the [`with` pattern](https://stackoverflow.com/questions/6159900/correct-way-to-write-line-to-file). – Mateen Ulhaq Jul 11 '18 at 01:08
  • `new_1` is the perfect example of how hard it is to name things in programming. This basically outlines the revision from putting the `f = open(“music.txt”, “a”) f.write(“2 Hours”)` inside the if statement. I changed it because I thought a function might do the trick but there was no change. – Jason White Jul 11 '18 at 01:09

4 Answers4

4

You're close! You don't need to do any int-to-string conversion in your if-statement. The following will work just fine:

if how_often == 1:
    add_file_1(new_1)
elif how_often == 2:
    add_file_2(new_2)
elif how_often == 3:
    add_file_3(new_3)
else:
    add_file_4(new_4)

As Brad Solomon mentioned, the reason it's not working is because how_often is an int, but you're comparing it to a string and they are not equal.

Visit https://repl.it/repls/ScaredGhostwhiteRegister to see this codin action. While the function won't actually load, you can see which function it's trying to call based on the input you provide.

Sheil Naik
  • 41
  • 4
0

Why would you use the func?

I don't think this code will be necessary.

If I were you, I would use file openings globally.

Source :

import time
import os
print("Welcome!")
name = input("Please enter your name: ")
print("Hello",name, "! I am going to guess your most favorite type of music.")
time.sleep(2)
print("Please, choose from one of the following: ")
listening_time = ["1 - One hour a day", "2 - About two hours per day", "3 - three to four hours per day", "4 - Most of the day"]
print(listening_time)
how_often = int(input("I find myself listening to music..."))

f =open("music.txt", "a") 
if how_often == 1:
    f.write("1 Hour")
elif how_often == 2:
    f.write("2 Hours")
elif how_often == 3:
    f.write("3 - 4 Hours")
else:
    f.write("Most of the day")

If I don't understand, let me know.

clem
  • 23
  • 5
0

You will get an error because you are comparing an integer and a string in how_often == str('1'):. But you can fix that like this: how_often == 1:.

You don't need to create a function add_file for different types of input you can create just one and make it generic:

def add_file(new_1, usr_hours_choise):
    with open("music.txt", "a") as f:
        f.write(usr_hours_choise)

and then in the if statement you assign it:

if how_often == 1:
    add_file(new_1, "1 hour.")
elif how_often == 2:
    add_file(new_2, "2 hour.")
elif how_often == 3:
    add_file(new_3, "3 - 4 hour.")
elif how_often == 1:
    add_file(new_4, "most of the day.")

This will save a lot of lines of code. But it is not clear what new_1 and what other ones do.

Muzol
  • 301
  • 1
  • 11
0

you could simplify things with a dictionary for the listening time options. And a good way to open files is to use a 'with' block.

print("Please, choose from one of the following: ")

listening_times = {1:"One hour a day", 
                   2:"About two hours per day", 
                   3:"Three to four hours per day", 
                   4:"Most of the day"}

for k in listening_times:
    print "    %d - %s" % (k, listening_times[k])

how_often = int(input("I find myself listening to music..."))

with open("music.txt", 'a') as f:
    f.write(listening_times[how_often])
Todd
  • 4,669
  • 1
  • 22
  • 30