繁体   English   中英

在Python中从另一个方法调用一个方法

[英]Calling one method from another method in Python

我意识到这段代码确实没有任何意义,但我只是在练习:)

基本上我想做的是,如果用户为生日输入一个空白名称,我希望它跳转到储蓄循环并运行该代码。

我意识到尽管以下内容不正确:

self.savings()

有任何想法吗?

谢谢

birthdays = {'Alice': 'Apr 1', 'Bob': 'Dec 12', 'Carol': 'Mar 4'}
while True:
    print('Enter a name: (blank to quit)')
    name = input()
    if name == '':
        self.savings()
    if name in birthdays:
        print(birthdays[name] + ' is the birthday of ' + name)
    else:
        print('I do not have birthday information for ' + name)
        print('What is their birthday?')
        bday = input()
        birthdays[name] = bday
        print('Birthday database is updated')


savings = {'401k': '100.00', 'RothIRA': '500.00', 'Savings': '350.00'}
while True:
    print('Enter an account: (blank to quit)')
    money = input()
    if money =='':
        break
    if money in savings:
        print((savings[money] + ' is the total amount in your ') + money)
    else:
        print('I do not have savings info for ' + money)
        print('How much money is in this account?')
        acct = input()
        savings[money] = acct
        print('Savings database is updated')

print(savings)

而不是self.savings()

if name == '':
    self.savings() 

您不能使用break来立即退出while循环吗? 然后应该继续进行下一个循环。

...我很确定那行得通。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM