Python Forum
General programming question (input string)[
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
General programming question (input string)[
#1
Big Grin 
#!/usr/bin/python3

list_item_two = ['apples', 'oranges', 'pears', 'apricots']

list_item_two.append("celery")

print("ADD (1) or REMOVE (2) elements\n")

user_input = input("> ")

if user_input == "1":
    print("Add Element to List")
    special_val = "add"
elif user_input == "2":
    print("Remove Element to List")
    special_val = "remove"

i = 1

while i < 4: # Output list
    for fruit in list_item_two:
      print(f"{i} fruit of type: {fruit}")
      i += 1

print("\nLength of elements: ", len(list_item_two))

input_val = input(f"What element to {special_val}? ") # if add or remove

# insert element
i = 1
while i < 2:
    if special_val == "add":
      list_item_two.insert(0, input_val)

    elif special_val == "remove":
      list_item_two.remove(input_val) # this works
      i += 1

print("Now the list is: ", list_item_two)
please, this line right here,

      list_item_two.insert(0, input_val)
Is there a better way to write so the code runs successfully, currently under Debian Linux the program is hanging after typing '1' (ADD'ing an element) after program execution. Thanks!
Reply
#2
Your insertion while loop (line 31) has no exit unless i >= 2.

But the "add" section does not modify i. So if you enter the loop with special_val == "add", there is no way to terminate.

I don't understand why there is a while loop there at all. Is there a case where you would expect to run the insertion or removal more than one time? Why not just get rid of the while?
Reply
#3
@bowlofred is correct. Remove the loop and you code performs as expected.
#!/usr/bin/python3
 
list_item_two = ['apples', 'oranges', 'pears', 'apricots']
 
list_item_two.append("celery")
 
print("ADD (1) or REMOVE (2) elements\n")
 
user_input = input("> ")
 
if user_input == "1":
    print("Add Element to List")
    special_val = "add"
elif user_input == "2":
    print("Remove Element to List")
    special_val = "remove"
 
i = 1
 
Output:
ADD (1) or REMOVE (2) elements > 1 Add Element to List 1 fruit of type: apples 2 fruit of type: oranges 3 fruit of type: pears 4 fruit of type: apricots 5 fruit of type: celery Length of elements: 5 What element to add? grapes Now the list is: ['grapes', 'apples', 'oranges', 'pears', 'apricots', 'celery']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using string input for boolean tronic72 3 702 Nov-01-2023, 07:48 AM
Last Post: Gribouillis
  String to List question help James_Thomas 6 987 Sep-06-2023, 02:32 PM
Last Post: deanhystad
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,135 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  syntax error question - string mgallotti 5 1,319 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  python can - general question caslor 0 1,120 Jul-14-2022, 05:21 PM
Last Post: caslor
  a general question barryjo 5 1,520 Feb-01-2022, 10:12 PM
Last Post: Gribouillis
  input function question barryjo 12 2,725 Jan-18-2022, 12:11 AM
Last Post: barryjo
  General Programming Question with Dictionary giddyhead 12 2,732 Jan-10-2022, 10:12 AM
Last Post: Pedroski55
Exclamation question about input, while loop, then print jamie_01 5 2,682 Sep-30-2021, 12:46 PM
Last Post: Underscore
  Question about change hex string to integer sting in the list (python 2.7) lzfneu 1 2,536 May-24-2021, 08:48 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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