Saya mencoba menjalankan modul dari konsol. Struktur direktori saya adalah ini:
Saya mencoba menjalankan modul p_03_using_bisection_search.py
, dari problem_set_02
direktori menggunakan:
$ python3 p_03_using_bisection_search.py
Kode di dalamnya p_03_using_bisection_search.py
adalah:
__author__ = 'm'
from .p_02_paying_debt_off_in_a_year import compute_balance_after
def compute_bounds(balance: float,
annual_interest_rate: float) -> (float, float):
# there is code here, but I have omitted it to save space
pass
def compute_lowest_payment(balance: float,
annual_interest_rate: float) -> float:
# there is code here, but I have omitted it to save space
pass
def main():
balance = eval(input('Enter the initial balance: '))
annual_interest_rate = eval(input('Enter the annual interest rate: '))
lowest_payment = compute_lowest_payment(balance, annual_interest_rate)
print('Lowest Payment: ' + str(lowest_payment))
if __name__ == '__main__':
main()
Saya mengimpor fungsi yang memiliki p_02_paying_debt_off_in_a_year.py
kode:
__author__ = 'm'
def compute_balance(balance: float,
fixed_payment: float,
annual_interest_rate: float) -> float:
# this is code that has been omitted
pass
def compute_balance_after(balance: float,
fixed_payment: float,
annual_interest_rate: float,
months: int=12) -> float:
# Omitted code
pass
def compute_fixed_monthly_payment(balance: float,
annual_interest_rate: float) -> float:
# omitted code
pass
def main():
balance = eval(input('Enter the initial balance: '))
annual_interest_rate = eval(
input('Enter the annual interest rate as a decimal: '))
lowest_payment = compute_fixed_monthly_payment(balance,
annual_interest_rate)
print('Lowest Payment: ' + str(lowest_payment))
if __name__ == '__main__':
main()
Saya mendapatkan kesalahan berikut:
ModuleNotFoundError: No module named '__main__.p_02_paying_debt_off_in_a_year'; '__main__' is not a package
Saya tidak tahu bagaimana menyelesaikan masalah ini. Saya sudah mencoba menambahkan __init__.py
file, tetapi masih belum berfungsi.
eval(input(...
bit disarankan oleh 2to3. Saya telah melakukannya untuk saya hari ini. senang saya tidak mengikuti saran itu blindling
eval(input...
mungkin bukan ide yang bagus. Saya hanya menguraikannya alih-alih membuka peluang untuk eksekusi kode arbitrer.