1

I have a string field and an array of numbers. When I iterate through the list I should get the string to append with the number from the array.

At the moment, it only returns the string + current position of the array. The following is my code. How could I solve this?

digit_list = list(map(int, str(extra_digits)))
pi_local = "PI"
for digit in range(len(digit_list)):
    pi_local = pi_local + str(digit_list[counter])
    pi_label.config(text = pi_local)

initial incorrect output

I have tried the suggestion below by trying to iterate over the list, but I am still not getting the right result. full code is below

pi = "PI"
extra_digits = "159265358979323846"


counter = 0
init = 2

#digit_list = list(map(int, str(extra_digits)))



def button_pressed():
    global counter
    global pi
    #global digit_list
    global init
    digit_list = list(map(int, str(extra_digits)))
    our_label.config(text="Pi to " + str(init+counter) + " decimals")
    pi_local = pi
    for digit in digit_list:
        pi_local = pi_local + str(digit)
        pi_label.config(text = str(digit))
        #pi_label.config(text = str(pi) + str(digit_list[counter]))
    counter = counter +  1

The output I am getting is Current incorrect output after using solution below

3
  • 4
    could you clarify with given input output example what you expected Commented Apr 26, 2020 at 9:38
  • 2
    What is ˋcounterˋ? Are you aware that you can directly iterate over a list, without indexing? Why do you convert your input to a list of integers, when you only need a string? Commented Apr 26, 2020 at 9:39
  • What is your expected output? Also, please add your observed output as text instead of pictures. Commented Apr 26, 2020 at 16:57

2 Answers 2

1
In [77]: pi_local = "PI"                                                                                                                                                                                    

In [78]: digit_list = [1,3,5,2]                                                                                                                                                                             

In [79]: [str(i) + pi_local for i in digit_list]                                                                                                                                                            
Out[79]: ['1PI', '3PI', '5PI', '2PI']
Sign up to request clarification or add additional context in comments.

Comments

0

You need to iterate through the elements in digit_list

digit_list = list(map(int, str(extra_digits)))
pi_local = "PI"
for digit in digit_list:
    pi_local = pi_local + str(digit)
    pi_label.config(text = pi_local)

2 Comments

Hi, My source data is as follows extra_digits = "159265358979323846" and I converted that to a list map as well using digit_list = list(map(int, str(extra_digits)))
My requirement is that each time I click on the Next Digit button it appends the digit at the particular counter to my string being printed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.