Home > other >  How do you add the stem onto leaves (python)? For example, if I input [3 1 5 6 7] then it should put
How do you add the stem onto leaves (python)? For example, if I input [3 1 5 6 7] then it should put

Time:10-08

So basically how do I add the stem onto the leaves?

If the user inputs multiple lines of input such as [3 1 5 6 7] and [4 1 2 3 4] separately (one after the other) it should come out as [31, 35, 36, 37, 41, 42, 43, 44] in another variable/string.

How do you do this? Help would be much appreciated.

This is what I have (probably not useful).

print('Enter stem and leaf data.')
  while True:
    yes = input('Enter row: ')
    yes1 = yes.split()
    for i in range(len(yes1)):
      # convert each item to int type
      yes1[i] = int(yes1[i])
    print(yes1)
  else:
    print('Next..')
    break

CodePudding user response:

Does it solve your problem?

print('Enter stem and leaf data.')
while True:
    yes = input('Enter row: ')
    if yes == '':
        break
    yes1 = yes.split()
    for i in range(1, len(yes1)):
        yes1[i] = int(yes1[0]   yes1[i])
    print(yes1[1:])

If you print an empty string, then the code finishs

CodePudding user response:

Try this One

print('Enter stem and leaf data.')
yes = ' '  # to break while loop
complete_data = []
while yes != '':    # if User press Enter Key without any character then loop ends
    yes = input('Enter row: ')
    yes1 = list(yes)   # convert string into list of characters
    yes1 = [int(str(yes1[0])   str(i)) for i in yes1[1:]] #Actual Logic
    #  first stars a for loop on yes1[1:] then add yes[1]   i(loop return) then convert to string

    complete_data = complete_data   yes1  # contains complete data
    print(yes1)

print(complete_data)

result

Enter stem and leaf data.

Enter row: 31567

[31, 35, 36, 37]

Enter row: 41234

[41, 42, 43, 44]

Enter row:

[31, 35, 36, 37, 41, 42, 43, 44]

  • Related