如何在Python的if语句中运行函数

时间:2018-07-11 00:45:17

标签: python

我正在尝试将输入中的变量信息添加到文本文档中。该文档应放在应有的位置。到目前为止,我已经有了这段代码:

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)

4 个答案:

答案 0 :(得分:3)

您接近了!您无需在if语句中进行任何int到字符串的转换。以下将正常工作:

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)

Brad Solomon mentioned一样,它不起作用的原因是因为how_often是一个整数,但是您正在将其与字符串进行比较并且它们不相等。

访问https://repl.it/repls/ScaredGhostwhiteRegister以查看此codin操作。虽然该函数实际上不会加载,但是您可以根据您提供的输入来查看它正在尝试调用哪个函数。

答案 1 :(得分:0)

为什么要使用该功能?

我认为此代码不是必需的。

如果我是你,我将在全球范围内使用文件打开方式。

来源:

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

如果我不明白,请告诉我。

答案 2 :(得分:0)

由于比较integer中的stringhow_often == str('1'):,将导致错误。但是您可以这样解决:how_often == 1:

您无需为不同类型的输入创建函数add_file,只需创建一个函数即可使其通用:

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

,然后在if语句中为其分配:

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

这将节省很多代码行。 但是,尚不清楚new_1做什么和其他其他做什么。

答案 3 :(得分:0)

您可以使用字典来简化收听时间选项。打开文件的好方法是使用“ with”块。

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