0

Can someone tell me if there is a problem with this code.It is supposed to take give the sum of consecutive numbers ?

It is given first and last numbers of a list of consecutive numbers and gives there sum: Code here: def find_sum(first_number, second_number): return((first_number + second_number )*(second_number/ 2)) num1=float(input("first number : ")) num2=float(input("second number ")) if num1 and num2==type(int): answer = find_sum(num1, num2) print('The sum of a list of numbers',) print ('('+num1+","+num2+')' , ) print ("is"+ answer) else: print("invalid input")

16th Sep 2021, 12:14 AM
Richard
Richard - avatar
1 Odpowiedź
0
Hi Richard! When you're trying to connect two conditions using logical operators you have to compare each variable separately. Here, you used float() to convert your input to a float but you're checking whether it's a integer or not. For that, you can check if they're floats or not. And, you can't add integers/floats with strings. Inorder to handle this problem, you can use str() function for them or use comma as concat operator. Here it is your corrected code. def find_sum(first_number, second_number): return((first_number + second_number )*(second_number/ 2)) num1=float(input("first number : ")) num2=float(input("second number ")) if type(num1) == float and type(num2) == float: answer = find_sum(num1, num2) print('The sum of a list of numbers') print ('(',num1,",",num2,')' ) print ("is",answer) else: print("invalid input")
16th Sep 2021, 1:23 AM
Python Learner
Python Learner - avatar