python - 我在无限循环的尝试中做错了什么?

标签 python loops infinite-loop

我正在尝试为我的 python 类编写代码,并且正在制作一个基本的钱包管理系统。但我无法让这个无限循环工作。我做错了什么?

def main():
# Initial balance
 balance = input ('Wallet balance: ')

# This constructs the infinite loop
while true:

    # Initial balance
     balance = input ('Wallet balance: ')

    # Withdraw amount from wallet balance
     withdraw= input ('Withdraw amount: ')

    #If there is no balance to withdraw from
     if balance < 1:
         print ('You have no funds to withdraw.')

    # Withdraw process and new balance display
     if balance > 0:
         new = balance - withdraw
         print ('Your new balance: '), new
main()

最佳答案

已为您修复,但有一些错误

Python 中 True 必须大写

如果要将 int 传递给 str,则需要将 int 转换为 str

另外,我不确定你为什么使用 main 函数?它没有将初始值传递到无限循环中,看起来是多余的。

def main():
    # Initial balance
    balance = input ('Wallet balance: ')

# This constructs the infinite loop
while True:

    # Initial balance
    balance = input('Wallet balance: ')

    # Withdraw amount from wallet balance
    withdraw= input('Withdraw amount: ')

    #If there is no balance to withdraw from
    if balance < 1:
        print('You have no funds to withdraw.')

    # Withdraw process and new balance display
    if balance > 0:
        new = balance - withdraw
        print('Your new balance: ' + str(new))
main()

但是,我假设您正在尝试构建一个可以取款和存款的功能钱包。我不确定您对 python 和类构建有多满意,但我构建了一个功能齐全的钱包供您查看和学习。

class Wallet:

    #Grab the initial balance in the account
    def __init__(self, initial_balance):
        self.balance = initial_balance

    #method that deposits funds to the balance
    def deposit(self, deposit_amount):
        self.balance += deposit_amount

    #method that attempts to withdraw from the balance
    def withdraw(self, withdraw_amount):
        if withdraw_amount <= self.balance:
            self.balance -= withdraw_amount
        else:
            print("Insufficient funds. You have " + str(self.balance) + " in your account. Please deposit more or withdraw less.")

    #display the balance
    def display_balance(self):
        print("The current balance in the account is " + str(self.balance))
        print("-----")

#Check if this module is the one currently being run
if __name__ == "__main__":
    #Ask user for initial balance
    initial_balance = int(input("Initial balance in account: "))
    #initiate the class, passing the initial balance
    wallet = Wallet(initial_balance)
    wallet.display_balance()
    while True:
        #Pick an option to withdraw, deposit or exit
        print("1: Deposit funds")
        print("2: Withdraw funds")
        print("3: Exit")
        type = int(input("Please select an option (1, 2 or 3): "))
        if type == 1:
            deposit_amount = int(input("Please specify amount to deposit: "))
            wallet.deposit(deposit_amount)
            wallet.display_balance()
        elif type == 2:
            withdraw_amount = int(input("Please specify amount to withdraw: "))
            wallet.withdraw(withdraw_amount)
            wallet.display_balance()
        elif type == 3:
            break
        else:
            print("Invalid selection, please type either 1, 2 or 3")

关于python - 我在无限循环的尝试中做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25516604/

相关文章:

java - 防止java中的无限循环

C++ 无限循环导致堆栈溢出错误?

python - 来自源代码的基本 Vim 安装

python - tf.estimator.Estimator 中参数的混淆

python - Django静态文件404错误

python - 为行中的每个值创建一个新列

linux - Bash 脚本,用于循环遍历一些带有名称更改的文件

python - pytest - monkeypatch 关键字参数默认

loops - 批量编程循环?

node.js - 如何超时终止 docker 容器?