簡體   English   中英

初學者嘗試調試一個使用其他兩個模塊才能運行的程序。 似乎無法定義變量

[英]BEGINNER Trying to debug a program that uses two other modules in order to run. Can't seem to define variables

我正在使用Python IDLE。 我正在嘗試創建一個如下的旅行計划器:

---------------------------
Welcome to  the Trip    Planner
---------------------------
Travel  Options
--------------
1.  Rome
2.  Berlin
3.  Vienna
Where   would   you like    to  go? 2
And how many    days    will    you be  staying in  Berlin? 5
Your    trip    to  Berlin  has been    booked!



Itinerary.txt
Trip    Itinerary
--------------
Destination:    Berlin
Length  of  stay:   5
Cost:   $100.80

運行后,該程序將生成一個名為itinerary.txt的文本文件。 我似乎遇到的麻煩是currency.py對我不起作用。 Python不斷告訴我定義euro_rate,並且當我嘗試出現許多問題時,這讓我感到沮喪,因為我只是一個初學者。

這是我在計划程序中可能需要修復的部分內容...

def main():
    # Show destinations
    destinations.print_options()

    # Pick destination
    choice = destinations.get_choice()

    # Get destination info
    destination = destinations.get_info(choice)

    # Calculate currency exchange
    dollar_rate = currency.convert_dollars_to_euros(euro_rate)

    # Calculate cost
    cost = dollar_rate + length_of_stay

我的整個貨幣程序看起來像:

def convert_euros_to_dollars(euro_rate):
    return (euro_rate * 1.12)

def convert_dollars_to_euros(dollar_rate):
    return (dollar_rate / 1.12)

在這一點上我迷路了,我需要一些額外的幫助。

Python告訴您定義euro_rate因為euro_rate未定義。 您需要在main()函數中將其定義為某物,或者需要將其實際值作為參數傳遞給convert_dollars_to_euro 當您沒有給程序提供在任何地方都可以使用的euro_rate時,轉換就不可能正常工作。

例如,如果您希望將euro_rate傳遞給convert_dollars_to_euro時使其等於2.0 ,則可以在main()的開頭定義變量,如下所示:

def main():

    #defining variables
    euro_rate = 2.0

    # Show destinations
    destinations.print_options()

    # Pick destination
    choice = destinations.get_choice()

    # Get destination info
    destination = destinations.get_info(choice)

    # Calculate currency exchange
    dollar_rate = currency.convert_dollars_to_euros(euro_rate)

    # Calculate cost
    cost = dollar_rate + length_of_stay

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM