我正在尝试创建此函数以将用户的2个正整数相乘。当我运行代码时,它基本上说列表元素是字符串而不是整数,即使我在输入循环中指定了我希望将字符串转换为整数。不确定是怎么回事吗? (n.b。我确实知道这完全是不必要的,但我是一个初学者,只是尝试学习一些基本的挑战。谢谢)
def multiply(a, b):
c = a * b
return c
input_list = []
for items in range(1, 3):
input1 = (input('Enter 2 positive, whole numbers you would like to multiply '))
int(input1)
input_list = input_list + [input1]
result = multiply(input_list[0], input_list[1])
print(result)
答案 0 :(得分:3)
您将input1转换为整数,但没有保存结果:
int(input1)
您必须这样做:
input1 = int(input1)
答案 1 :(得分:0)
def multiply(a, b):
c = a * b
return c
input_list = []
for items in range(1, 3):
input1 = (input('Enter 2 positive, whole numbers you would like to multiply '))
input_list.append(int(input1))
result = multiply(input_list[0], input_list[1])
print(result)
尝试一下。
答案 2 :(得分:0)
只需将字符串"replace$
和int(a)
转换为整数:
int(b)
答案 3 :(得分:0)
怎么样呢?
def multiply(a, b):
c = a * b
return c
numbers = []
for items in range(2):
string = input('Enter a number you would like to multiply: ')
number = int(string)
numbers.append(number)
result = multiply(numbers[0], numbers[1])
print(result)