Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble creating loops
#1
I am trying to figure out what's wrong with the code. It's supposed to be a function that counts how many times the sum of some string's elements are equal to an input number. No other libraries are supposed to be used.
For example:
string='1,2,3'
number=3
Output:
2

def calculate(string, number):
    string=''
    result=0
    new_list=[]
    new_list=[int(x) for x in string.split(',')]
    for i in range(0, len(new_list)+1,-1): 
            sum(new_list)
            if sum(new_list)==number: 
                result+= 1    
            else:
                continue
            i+=1   
    return result
Error:
ValueError: invalid literal for int() with base 10: ''
Reply


Messages In This Thread
Trouble creating loops - by Den - Oct-19-2019, 07:36 AM
RE: Trouble creating loops - by ibreeden - Oct-20-2019, 12:43 PM
RE: Trouble creating loops - by Den - Oct-20-2019, 01:34 PM
RE: Trouble creating loops - by ibreeden - Oct-23-2019, 05:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help with creating dynamic columns with for loops for stock prices PaDat 2 926 Feb-22-2023, 04:34 AM
Last Post: PaDat
  Trouble with While loops jj432 1 1,862 Oct-19-2019, 01:22 AM
Last Post: Aurthor_King_of_the_Brittons
  Troubble creating loops in PyQt LavaCreeperKing 0 5,557 Mar-02-2017, 08:05 PM
Last Post: LavaCreeperKing

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020