【发布时间】:2021-11-27 19:02:14
【问题描述】:
如何获取代码以将 set_budget 函数中获得的 budget_amt 用于其他函数?我尝试使用 global,即使不建议哪个适用于第一个代码。我在使用返回函数时遇到问题。我不确定如何让我的预算金额用于我的 new_transaction 函数。我是编码新手,并且绞尽脑汁思考如何完成这项工作。请帮忙!
from datetime import datetime
import pandas as pd
def set_budget():
global budget_amt
try:
budget_amt=float(input("Please enter a budget amount for your monthly expenditure:\n$"))
if budget_amt<=0:
budget_amt=float(input("Please enter a positive value:\n$"))
except ValueError:
print("Sorry! You Probably entered an invalid number or letter(s). Please enter a number:\n$")
budget_amt=float(input("Please enter a budget amount for your monthly expenditure:"))
confirmation1=input(f"Are you sure you want to set a monthly budget of ${budget_amt} ? \n[Y/N]:")
if confirmation1.lower()=='y':
print(f"Your have set a monthly budget of ${budget_amt}.")
#add to file username, budget amt
elif confirmation1.lower()=='n':
set_budget()
else:
confirmation1=input(f"Are you sure you want to set a monthly budget of ${budget_amt}? \n[Y/N]:")
if confirmation1.lower()=='y':
print(f"Your have set a monthly budget of ${budget_amt}.")
#add to file username, budget amt
elif confirmation1.lower()=='n':
set_budget()
def set_warning():
global warning_value
limit_choice=input("Would you like to set a warning when a certain amount of your budget has been spent? \n[Y/N]:")
if limit_choice.lower()=='y':
print(f"You have previously set a monthly budget of ${budget_amt}.")
try:
warning_value=float(input("Please enter the amount at which you would like to receive a warning at:\n$"))
except ValueError:
print("Please enter a number")
warning_value=float(input("Please enter the amount at which you would like to receive a warning at:\n$"))
confirmation2=input(f"You will receive a warning when your total monthly expenditure reaches ${warning_value}. Are you sure?\n [Y/N]:")
if confirmation2.lower()=='n':
set_warning()
elif confirmation2.lower()=='y':
print(f"You will receive a warning when your total monthly expenditure reaches ${warning_value}.")
#add to csv file (warning_value)
elif confirmation2.lower()!='n' or 'y':
confirmation2=input(f"You will receive a warning when your total monthly expenditure reaches ${warning_value}. Are you sure?\n [Y/N]:")
elif limit_choice.lower()=='n':
confirmation3=input("Are you sure?\n [Y/N]:")
if confirmation3.lower()=='n':
set_warning()
elif confirmation3.lower()=='y':
print("You have chosen not to set a warning value.")
warning_value=0
elif limit_choice.lower()!='y' or 'n':
set_warning()
return warning_value
set_warning()
return budget_amt
set_budget()
def new_transaction():
nonlocal budget_amt
print("New Transaction")
date = datetime.today()
data = []
try:
transaction_amt = float(input("Please enter the transaction amount:\n$ "))
except ValueError:
print("Please enter a number")
transaction_amt = float(input("Please enter the transaction amount:\n$ "))
choice = input("Is this an Income or Expense?\n [I/E]")
if choice.lower() == "i":
category = "Income"
remarks = "NA"
confirmation = input("Are you sure you want to input this transaction?\n [Y/N]: ")
while confirmation.lower() != 'y':
print("Transaction has been cancelled.")
new_transaction()
print("You have successfully entered the transaction.")
if choice.lower() == "e":
print("""
List of available categories:
Dining
Grocery
Entertainment
Others
""")
category = input("Please type the category from the list above: ")
remarks = input("Remarks if any, else put NA: ")
confirmation = input("Are you sure you want to input this transaction? [Y/N]: ")
while confirmation.lower() != 'y':
print("Transaction has been cancelled.")
new_transaction()
while budget_amt >= 0:
if transaction_amt > budget_amt:
print("You have exceeded the budget")
else :
budget_amt -= transaction_amt
print("You have successfully entered the transaction.")
print("Balance remaining: ", budget_amt)
print("You have no budget left for the month.")
data.append([date,budget_amt, transaction_amt,remarks,category])
storeValuesAsDF(data)
return data
def storeValuesAsDF(data):
df = pd.DataFrame(data, columns = ['Username','Date', 'Budget', 'Final Amount', 'Remarks', 'Category'])
saveAsCSV(df)
return
def saveAsCSV(df):
df.index += 1
df.to_csv(index=True)
以下所有内容仅供我发布抱歉给您带来的不便 Python 是一种多范式编程语言。完全支持面向对象的编程和结构化编程,它的许多特性支持函数式编程和面向方面的编程(包括元编程[57]和元对象(魔术方法))。 [58]通过扩展支持许多其他范例,包括按合同设计[59][60] 和逻辑编程。[61]
Python 使用动态类型以及引用计数和循环检测垃圾收集器的组合来进行内存管理。[62]它还具有动态名称解析(后期绑定)功能,可在程序执行期间绑定方法和变量名称。
Python 的设计为 Lisp 传统中的函数式编程提供了一些支持。具有filter、map和reduce功能;列表推导、字典、集合和生成器表达式。[63]标准库有两个模块(itertools 和 functools),它们实现了从 Haskell 和 Standard ML 借来的功能工具。[64]
该语言的核心哲学在文档 The Zen of Python (PEP 20) 中进行了总结,其中包括以下格言:[65]
美丽胜于丑陋。 显式优于隐式。 简单胜于复杂。 复杂胜于复杂。 可读性很重要。 Python 没有将其所有功能都内置到其核心中,而是被设计为高度可扩展的(带有模块)。这种紧凑的模块化使其特别流行,作为向现有应用程序添加可编程接口的一种方式。 Van Rossum 对具有大型标准库和易于扩展的解释器的小型核心语言的愿景源于他对支持相反方法的 ABC 的挫败感。[38]
Python 力求提供更简单、更简洁的语法和语法,同时为开发人员提供编码方法的选择。与 Perl 的“有不止一种方法来做到这一点”的座右铭相反,Python 信奉“应该有一种——最好只有一种——明显的方法来做到这一点”的设计理念。 [65] Python 软件基金会研究员兼 Python 书籍作者 Alex Martelli 写道:“在 Python 文化中,将某事物描述为‘聪明’并不被视为恭维。”[66]
Python 的开发人员努力避免过早的优化,并拒绝对 CPython 参考实现的非关键部分进行补丁,这些补丁会以清晰度为代价提供边际速度的提升。[67]当速度很重要时,Python 程序员可以将时间关键函数转移到用 C 等语言编写的扩展模块中,或者使用即时编译器 PyPy。 Cython 也可用,它将 Python 脚本翻译成 C 语言,并将 C 级 API 直接调用到 Python 解释器中。
Python 的开发人员旨在使该语言易于使用。这反映在它的名字上——向英国喜剧团体 Monty Python[68] 致敬——以及偶尔有趣的教程和参考资料,例如引用垃圾邮件和鸡蛋的示例(对 Monty Python 草图的引用)标准的 foo 和 bar。[69][70]
Python 社区中一个常见的新词是 pythonic,它可以具有与程序风格相关的广泛含义。说代码是 pythonic 就是说它很好地使用了 Python 习语,它是自然的或表现出语言的流畅性,它符合 Python 的极简主义哲学和对可读性的强调。相比之下,难以理解或读起来像从另一种编程语言粗略转录的代码称为 unpythonic。[71][72]
Python 的用户和崇拜者,尤其是那些被认为知识渊博或经验丰富的人,通常被称为 Pythonistas。[73][74]
【问题讨论】:
-
请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。
标签: python python-3.x python-2.7 encoding spyder