Jump to content

This small issue took me 3 days and i stil did not fix it. I was assigned to Create a program that counts the digits(0-9) from a string as well as other characters(not defined[probably all]). Your program will output a 11character string,that will count how many numbers it came across in the input. The first character would be the number of ZEROS python read in the string,the second one will do the same job but will show how many ONES it saw and ETC. The 11th character will be how many NON numbers are inside my string. If a number exceeds the 9time you should mark it with an ASTERISK. For example. INPUT=01710 OUTPUT=22000001000 

And i get this error EVERYTIME TypeError: must be str, not int. Take a look at my code.

user_data_list=[]
user_data=input()
while user_data!="end" and user_data!="END":
  met_data_list=[0,0,0,0,0,0,0,0,0,0,0]
  user_data_list=list(user_data)
  for i in range (len(user_data_list)):
    if user_data_list[i]=="0":   
      met_data_list[0]=met_data_list[0]+1
      
    elif user_data_list[i]=="1":
      met_data_list[1]=met_data_list[1]+1
      
    elif user_data_list[i]=="2":
     met_data_list[2]=met_data_list[2]+1
     
    elif user_data_list[i]=="3":
      met_data_list[3]=met_data_list[3]+1
      
    elif user_data_list[i]=="4":
       met_data_list[4]=met_data_list[4]+1
       
    elif user_data_list[i]=="5":
      met_data_list[5]=met_data_list[5]+1
      
    elif user_data_list[i]=="6":
      met_data_list[6]=met_data_list[6]+1
      
    elif user_data_list[i]=="7":
     met_data_list[7]=met_data_list[7]+1
     
    elif user_data_list[i]=="8":
     
     met_data_list[8]=met_data_list[8]+1
     
    elif user_data_list[i]=="9":
      
      met_data_list[9]=met_data_list[9]+1
     
    elif "0123456789" not in user_data_list[i]:
      met_data_list[10]=met_data_list[10]+1

    for i in range(len(met_data_list)):
      if met_data_list[i]>9:
       met_data_list.pop(i)
       met_data_list.insert(i,"*")
    
    

  print ("{}{}{}{}{}{}{}{}{}{}{}".format(met_data_list[0],met_data_list[1],met_data_list[2],met_data_list[3],met_data_list[4],met_data_list[5],met_data_list[6],met_data_list[7],met_data_list[8],met_data_list[9],met_data_list[10]))

  user_data=input()

I really need help. My head will explode

 

Link to comment
https://linustechtips.com/topic/989003-i-need-help-with-this-python-problem/
Share on other sites

Link to post
Share on other sites

I get the wrong answer based on your sample input (22000001001 instead of the expected 22000001001), but the code runs without error using that link, so i'm not sure where you were getting a typeerror

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to post
Share on other sites

You're probably trying to print a number, in which cast you need to first call str([variable name]) if using Python 2.7, repr ([variable name]) if using Python 3

 

EDIT: Actually printing wouldn't matter. But either way.

Link to post
Share on other sites

1 minute ago, M.Yurizaki said:

You're probably trying to print a number, in which cast you need to first call str([variable name]) if using Python 2.7, repr ([variable name]) if using Python 3

Inside my for loop with the "*"  i added str(),but no progress. I want to print numbers yes.

 

4 minutes ago, fizzlesticks said:

Replacing the count with a * only needs to be done once, not every time through the main loop. After you get to 10 of some character you're trying to add '*' + 1.

if i remove the for loop from the while the programm will hang

Link to post
Share on other sites

6 minutes ago, FreQUENCY said:

if i remove the for loop from the while the programm will hang

You have 1 for loop inside your other for loop, it should be outside that loop. Also if nesting loops you shouldn't use the same variable name for both.

1474412270.2748842

Link to post
Share on other sites

image.png.90f828d99e5ef4b47f61b68bd627257c.pngimageproxy.php?img=&key=17c10421afdc6db8imageproxy.php?img=&key=17c10421afdc6db8

image.png.c70c1a18dfa85bab783163db6d27b564.png

10 minutes ago, fizzlesticks said:

You have 1 for loop inside your other for loop, it should be outside that loop. Also if nesting loops you shouldn't use the same variable name for both.

 

Are we on the same page? Cause i think i might be missing something. EDIT! Ignore the while exit loop! I tested something

Link to post
Share on other sites

25 minutes ago, FreQUENCY said:

Are we on the same page? Cause i think i might be missing something. 

I think so but it's hard to tell from the pictures. Do you still get the error after you moved the loop?

 

And for adding the '*'s you don't need to do a pop and insert you can just change the value directly.

met_data_list[j] = '*'

1474412270.2748842

Link to post
Share on other sites

DONE! Finished. Thank you all for the help!

text_list = []
user_data=input()
while user_data!="end" and user_data!="END":
  met_data_list=[0,0,0,0,0,0,0,0,0,0,0]

  for char in user_data:
    if char.isdigit():
        idchar=int(char)
        met_data_list[idchar] += 1
    else:
        met_data_list[10] += 1

  for i in range(len(met_data_list)):
    if met_data_list[i]>9:
     met_data_list.pop(i)
     met_data_list.insert(i,"*")
       
    
    

  print ("{}{}{}{}{}{}{}{}{}{}{}".format(met_data_list[0],met_data_list[1],met_data_list[2],met_data_list[3],met_data_list[4],met_data_list[5],met_data_list[6],met_data_list[7],met_data_list[8],met_data_list[9],met_data_list[10]))
  #print(''.join(text_list))
  user_data=input()

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×