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

如何在Python中的if语句中运行函数,python,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 on

我试图将输入中的变量信息添加到文本文档中。文件放在它应该放的地方。到目前为止,我有以下代码:

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)

你接近了!在if语句中不需要进行任何int到string的转换。以下方法可以很好地工作:

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)
同样,它不起作用的原因是因为
通常是int,但您将其与字符串进行比较,它们并不相等


请访问以查看此代码的运行情况。虽然函数不会实际加载,但根据您提供的输入,您可以看到它正在尝试调用哪个函数。

您已经接近了!在if语句中不需要进行任何int到string的转换。以下方法可以很好地工作:

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)
同样,它不起作用的原因是因为
通常是int,但您将其与字符串进行比较,它们并不相等


请访问以查看此代码的运行情况。虽然函数不会实际加载,但根据您提供的输入,您可以看到它试图调用哪个函数。

为什么要使用该函数

我不认为这个代码是必要的

如果我是你,我会全局使用文件打开

资料来源:

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

如果我不明白,请告诉我

为什么要使用func

我不认为这个代码是必要的

如果我是你,我会全局使用文件打开

资料来源:

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

如果我不明白,请告诉我

您将得到一个错误,因为您正在比较
中的
整数和
字符串的频率==str('1'):
。但您可以这样解决:
频率==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.")
这将节省大量代码行。
但是现在还不清楚什么是
新的\u 1
和其他的做什么。

您将得到一个错误,因为您在
中比较
整数和
字符串的频率==str('1'):
。但您可以这样解决:
频率==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.")
这将节省大量代码行。
但是现在还不清楚什么是
新的\u 1
和其他的做什么。

你可以用一本字典来简化听力时间选项。打开文件的一个好方法是使用“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])

你可以用字典来简化听力时间选项。打开文件的一个好方法是使用“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])


那么你具体的问题是什么呢?看起来你把
的频率定义为int,然后测试它与string的相等性。我尝试将它改为string,进行字符串比较,结果出现了一个错误。之前(使用上面的代码),我不会得到任何错误。它对文本文件没有任何作用。问题是:如何更改代码以执行某些操作。我没有错误的方式。它对文本文件没有任何作用。我上一个版本的代码,我试着把
f=open(“music.txt”,“a”)f.write(“2小时”)
放在if语句本身中,但没有结果。什么是
new\u 1
?你具体的问题是什么?看起来你把
多久一次的
定义为int,然后测试它与strings的相等性。我尝试将它更改为字符串,以进行字符串比较,但我得到了一个错误。之前(使用上面的代码),我不会得到任何错误。它对文本文件没有任何作用。问题是:如何更改代码以执行某些操作。我没有错误的方式。它对文本文件没有任何作用。我上一个版本的代码尝试将
f=open(“music.txt”,“a”)f.write(“2小时”)
放入if语句本身,但没有得到结果。什么是
new\u 1
?我现在遇到一个错误,没有定义
add\u file\u 2
。虽然
add_file_1
没有错误注释更新,但我按了(1-4)键,得到了“Not Defined”错误。我现在得到了一个
add_file_2
没有定义的错误。虽然
add_file_1
没有收到任何错误注释更新,但我按了(1-4)键,得到了“Not Defined”错误。这非常有效。谢谢你的帮助!这很有效。谢谢你的帮助!当链接只有一个条件为真的“如果”条件时,最好使用“elif”。我忘了这一点,谢谢。当链接只有一个条件为真的“如果”条件时,最好使用“elif”。我忘了这一点,谢谢。