为什么我不能打印我刚定义的变量?

2024-04-24 00:15:49 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有一个程序,它有一个可以由用户编辑的变量。名为“price”的变量默认设置为2。我试图在句子中打印变量,但它不起作用。我的代码如下:

price = 2

#Definition of 'task_1'.
def task_1():

    #Welcomes the user to the OCR car park.
    print("Welcome to the OCR car park.")

    #Asks for a £2 ticket fee and stores the amount
    #given in a variable called 'ticket_fee'.
    ticket_fee = int(input("Please insert a £",price,"ticket fee: "))

Tags: theto代码用户程序编辑parktask
3条回答

我不认为你需要一个输入,因为它没有任何进一步的工作,在那里,我们可以判断你的代码。 第二,输入不是用来打印任何东西的,它只是给用户一个提示(指令)。你应该避免这样。 使用print()调用这些类型的函数,或者您可以尝试在input()print()中打印字符串,并在下一行中使用int(input("Whatever you want"))(如果您确实需要用户的输入)。祝你好运!你知道吗

input()函数只接受一个参数:要打印的字符串。如果要内联打印变量,请尝试以下操作:

print("Please insert a £",price,"ticket fee: ")
ticket_fee = int(input())
ticket_fee = int(input("Please insert a £" + str(price) + " ticket fee: "))

价格应该是一个字符串。+应用于串联。你知道吗

相关问题 更多 >