python - python3上的简单书店程序

标签 python function user-input

我正在学习 Python,并尝试使用函数和用户输入编写一个简单的书店程序。它需要参数(书籍、价格)并打印“订单:'您的书籍选择'成本'x美元'”但我无法让它工作。你能检查一下我的代码并帮助我吗?

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

最佳答案

您正在传递两个未使用的参数,并且无论如何都不会初始化它们。他们是多余的。作品如下:

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())

给予:

Enter books title: Good Python
Enter books price: 2.30
Title: Good Python, costs 2.30

关于python - python3上的简单书店程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48977374/

相关文章:

python - 在 Python 的四循环中使用 readline() 时,如何忽略 .csv 文件中的空列?

python - Discord.py 检查 Channel 是否为 DM

python - 我的 thrift ./configure 不构建 python 库

函数构成 AND 函数应用

c - strlen(s) 和 (strlen)(s) 之间有什么区别吗?

c - 近似Pi/Taylor级数;提示用户输入

python - 将数据帧转换为 numpy 数组?

c++ - 成员函数可以通过哪些方式相互比较?

python - 如何读取多条原始输入?

c++ - 使用 cin 安全地提示是/否