I'm learning Python and I'm trying to write a simple bookstore program using functions and user input. It takes parameters (book, price) and prints "order: ' your book choice' costs 'x dollar' " but I can't get it to work. Could you check out my code and help me out?
def book_store(book,price):
book_choice = input("Enter books title: ")
book_price = input ("Enter books price: ")
return "Title: " + book_choice + ", costs " + book_price
print (book_store(book_choice, book_price))
NameError Traceback (most recent call last) in () 10 11 ---> 12 print (book_store(book_choice, book_price))
NameError: name 'book_choice' is not defined
Your code is redundant right now, it can either be:
def book_store():
book_choice = input("Enter books title: ")
book_price = input("Enter books price: ")
return "Title: " + book_choice + ", costs " + book_price
print(book_store())
OR:
def book_store(book, price):
return "Title: " + book + ", costs " + price
book_choice = input("Enter books title: ")
book_price = input("Enter books price: ")
print (book_store(book_choice, book_price))
Both versions work.
Also make sure you are using python 3, otherwise input does not return a string.