首页 > 解决方案 > Python3新手需要建议

问题描述

大家好,总的来说,我是编码和后端计算的新手。我已经做了大约 3 周,并认为我会尝试编写我的第一个应用程序。我的大多数应用程序都运行良好,但我无法弄清楚如何让系统接受新输入并将其应用于我的函数 loop() 运行后。请帮忙!

from os import sys

def say_bye():
    print("GoodBye!")
    sys.exit()

def loop():
    print()
    print("Would you like to enter another value?")
    choice2 = input('y/n: ')
    if choice2 == 'y':
        print()
        amount = float(input("enter another price: "))
        rate = float(input("tax rate: "))
        print()
        return in_page(), tot_with_tax_exit(), print(tot_with_tax()), loop()
    else:
        return say_bye()

def tot_with_tax():
    tax = (amount * rate)
    total = ("Your total after tax is", tax + amount)
    return total

def tot_with_tax_exit():
    if amount == int(0):
        print('would you like to exit')
        choice = input('y/n?: ')
        if choice == 'y':
            print()
            return say_bye()
        else:
            return tot_with_tax()

def in_page():
    print()
    print("Welcome to the tax tool!")
    print()
    print("Please enter your total price, and tax rate.")

in_page()
amount = float(input("enter price: "))
tot_with_tax_exit()
rate = float(input("tax rate: "))
print(tot_with_tax())
loop()

标签: python

解决方案


您必须将值传递给函数 to_with_tax() 这里是代码,其中包含使其工作的更改。

from os import sys

def say_bye():
    print("GoodBye!")
    sys.exit()

def loop():
    print()
    print("Would you like to enter another value?")
    choice2 = input('y/n: ')
    if choice2 == 'y':
        print()
        amount = float(input("enter another price: "))
        rate = float(input("tax rate: "))
        print()
        return in_page(), tot_with_tax_exit(), print(tot_with_tax(amount, rate)), loop()
    else:
        return say_bye()

def tot_with_tax(amount, rate):
    tax = (amount * rate)
    total = ("Your total after tax is", tax + amount)
    return total

def tot_with_tax_exit():
    if amount == int(0):
        print('would you like to exit')
        choice = input('y/n?: ')
        if choice == 'y':
            print()
            return say_bye()
        else:
            return tot_with_tax()

def in_page():
    print()
    print("Welcome to the tax tool!")
    print()
    print("Please enter your total price, and tax rate.")

in_page()
amount = float(input("enter price: "))
tot_with_tax_exit()
rate = float(input("tax rate: "))
print(tot_with_tax(amount, rate))
loop()

推荐阅读