-3

以下は私のコードです。距離が必要な場所を含むリストを作成することになっています。ユーザーは 2 つの異なる場所を選択して、2 つの場所の距離をマイルで表示する必要があります。その後、キロメートルに変換するオプションが表示されます。ただし、コードを完成させて機能させた後、「ユーザーがplace1、place2、またはplace 3を入力した場合、ユーザーに2番目の場所を入力するように求め続け、それ以外の場合はプログラムを再起動する」というようなifステートメントを追加しました。最初に戻りますが、if ステートメントが正しく記述されておらず、「2 番目が定義されていません」と表示されます。

print 'The available places to select from are:'

print 'place1, place2, place3: '


place1 = 50
place2 = 40
place3 = 30
Convert = 1.609344



First = str(raw_input('Please select from the list where you are coming from'))
if raw_input == 'place1' 'place2' 'place3':
    Second = str(raw_input('Please select where you are going to: '))
else:
    print 'please press any key to restart'

Distance = First + Second

print "the distance between the two places are", Distance, "miles"



Kilometres = bool(input('Would you like to convert to Kilometres? please type True to convert or False to exit: '))
if True:
        print 'The distance in Kilometres is',First + Second / Convert, 'Kilometres'

else:
        print 'press any ket to terminate'
4

1 に答える 1

7

if ステートメントをインデントしないでください。

First = int(input('Please select from the list where you are coming from'))
if answer in ['place1', 'place2', 'place3']:
    Second = int(input('Please select where you are going to: '))
else:
    print 'please press any key to restart'

それ以外の

First = int(input('Please select from the list where you are coming from'))
    if answer in ['place1', 'place2', 'place3']:
        Second = int(input('Please select where you are going to: '))
    else:
        print 'please press any key to restart'

編集:

これには助けが必要なようです。コードを作り直し、コメントを追加して、うまくいけば問題を解決しました。

#using a dictionary allows us to associate, or hash, a string with a value
place_dict = {"place1":50,"place2":40,"place3":30}

convert = 1.609344

def run_main():
    #placing the bulk of the program inside of a function allows for easy restarting
    print 'The available places to select from are:'
    print 'place1, place2, place3: '

    first = raw_input('Please select from the list where you are coming from: ')
    second = raw_input('Please select where you are going: ')
    #the values the user puts in are now stored in variables called 'first' and 'second', so if the user inputs "one", then first == "one"

    if first not in place_dict or second not in place_dict:
        #check to ensure that both first and second are in the place place dictionary
        #if not, then return none to the main program
        raw_input('Press any key to restart...')
        return None

    #an else is not needed here, because if the if statement executes, then we will not reach this point in the program

    #this returns the dictionary value of the first value that the user inputs + the second.
    return place_dict[first] + place_dict[second]


if __name__ == "__main__":
    #this line says that if we are running the program, then to execute.  This is to guard against unwanted behavior when importing

    distance = None
    #set an initial variable to None

    while distance is None:
        #run the main program until we get an actual value for distance
        distance = run_main()

    print "the distance between the two places are", distance, "miles"


    kilometres = bool(input('Would you like to convert to Kilometres? please type True to convert or False to exit: '))
    if kilometres == True:
        print 'The distance in Kilometres is',distance / convert, 'Kilometres'

    else:
        print 'press any key to terminate'    
于 2013-07-14T21:22:25.153 に答える