Python - необходимо рассчитать стоимость на основе пользовательского ввода - PullRequest
0 голосов
/ 21 сентября 2019

Мне нужно рассчитать стоимость на основе ввода пользователя и использования массива.У меня есть приведенный ниже код, но я продолжаю получать ошибки (направленные на мои «печатные» скобки).

Знаете ли вы, чего мне не хватает, и можно ли использовать лучший массив?

#route type
yourUserInput = input("will you use route 1 or 2 ")

finance = 1 #
if yourUserInput == "1":
    finance = 25
elif yourUserInput == "2":
    finance = 35
else:
    print("you did not enter a valid route")

print ("total cost" (cost))
# ticket type
tickettype = input("what type of ticket would you like (single or return) ")
price = 1 #
if tickettype == "single" or tickettype == "Single":
    price = 25
elif tickettype == "return" or tickettype == "Return":
    price = 35
else:
    print("you did not enter a valid ticket type")

#cost = int( finance ) *int( price )


ar= (finance + price)
#print "the total is therefore",
print ("your total price is" int(ar))

input("press enter to exit the program")

1 Ответ

0 голосов
/ 21 сентября 2019

В Python, когда вы хотите включить более одной переменной или текста в свой вывод, вы добавляете его в свой выходной оператор с запятой (,).Это похоже на то, как вы добавили бы + в Java в своем операторе вывода.

print ("total cost" (cost)) должно быть print ("total cost", cost), а print ("your total price is" int(ar)) должно быть print ("your total price is", int(ar))

Внедрение массива в ваш код будет выглядеть примерно так, если два значения в массиве равны 25 и35.

yourUserInput = input("will you use route 1 or 2 ")

cost = 1
finance = 1
price = 1
list_values = [25,35]

if yourUserInput == "1":
    finance = list_values[0]
elif yourUserInput == "2":
    finance = list_values[1]
else:
    print("you did not enter a valid route")

print ("total cost = ", cost)
# ticket type
tickettype = input("what type of ticket would you like (single or return) ")

if tickettype == "single" or tickettype == "Single":
    price = list_values[0]
elif tickettype == "return" or tickettype == "Return":
    price = list_values[1]
else:
    print("you did not enter a valid ticket type")

cost = finance * price
ar = (finance + price)

#print "the total is therefore",
print ("your total price is", ar)

input("press enter to exit the program")

Я советую вам прочитать https://docs.python.org/3/whatsnew/3.0.html, чтобы лучше понять основы Python.

...