Py学习  »  Sultan Singh Atwal  »  全部回复
回复总数  2
7 年前
回复了 Sultan Singh Atwal 创建的主题 » 使用input()函数时出现Python错误[TypeError:'str'对象不可调用]

试试这个:

notes =[]
def newNote(notes):
    note = input("Whats up")
    notes.append(note)
    return notes

inp = input("in or out? ")
if (inp == "in"):
    newNote(notes)

7 年前
回复了 Sultan Singh Atwal 创建的主题 » 如何在python中将input[by input()]值作为参数传递?

你可以试试这个:

def add(num1,num2):
    return num1 * num2

num 1 = int(input('Enter number1: '))
num2 = int(input('Enter number2: '))

print(add(num1,num2))

input函数将输入存储为字符串,因此代码中发生的情况是您正在输入两个整数,但它们存储为字符串。两个字符串不能相乘。您只需使用int()函数将输入字符串转换为整数。如果要乘浮点数,可以使用float()函数代替int()函数。在将字符串传递给函数后,还可以将其转换为整数或浮点数。像这样的:

def add(num1,num2):
    return int(num1) * int(num2)

num1=input('Enter number1: ')
num2=input('Enter number2: ')

print(add(num1,num2))