Python Programming sequence element replacement

Viewed 52

I need help, I have to create a program that uses a list display to create a list containing the integers from 1 to 10 in increasing order, and output the list. Then ask the user to enter a number between 1 and 10, replace this number with the missing str object, and output the list. If the user enters a string that does not represent an integer between 1 and 10, it instead generates the appropriate message indicating this situation, as shown in the sample runs:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Enter an integer between 1 and 10 >5
[1, 2, 3, 4, 'gone', 6, 7, 8, 9, 10]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Enter an integer between 1 and 10 >15
15 is not between 1 and 10

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Enter an integer between 1 and 10 >hello
hello is not a positive integer

I have created the following code:

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
print(my_list)
a = input('Enter an integer between 1 and 10 >')
b = (a.isalpha())
if int(a)<10 and b == False:
    c = my_list.index(int(a))
    d = my_list.remove(int(a))
    e = my_list.insert(c, 'gone') 
    print(my_list)

elif int(a) and b == False:
    b == True
    print (a, 'is not between 1 and 10')     
else:
     print(str(a), 'is not a positive integer')

but i got the following error:

RuntimeErrorElement(RuntimeError,Error on line 6:
    c = my_list.index(int(a))
ValueError: 0 is not in list

)

Does anyone know how I could solve this problem?

3 Answers

Your short-circuiting is wrong; and always evaluates the first condition. It only the second condition, given that the first one is False.

To resolve, change the control flow structure to:

if b == False and int(a)<10:
    c = my_list.index(int(a))
    d = my_list.remove(int(a))
    e = my_list.insert(c, 'gone') 
    print(my_list)
elif b == False and int(a):
    b == True
    print (a, 'is not between 1 and 10')     
else:
     print(str(a), 'is not a positive integer')

The list can be assigned directly, here you also need to add a condition to determine whether the number is between 1 and 10,try this:

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
print(my_list)
a = input('Enter an integer between 1 and 10 >')

if a.isdigit():
    num = int(a)
    if 1 <= num <= 10:
        index = my_list.index(int(a))
        my_list[index] = "gone"
        print(my_list)
    else:
        print(a, 'is not between 1 and 10')
else:
    print(a, 'is not a positive integer')

I feel you are overdoing the logic. Since its python, its quite easy to do it in lesser lines of code and easy condition checks. See an example below:

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(my_list)
a = input('Enter an integer between 1 and 10 >')
try:
    a = int(a)  # Convert string to integer, if its invalid, it raises an exception
    if 1 <= a <= 10 and a in my_list:  # Check for membership and range
        my_list[my_list.index(a)] = "gone"  # replace once
        print(my_list)
    else:
        print(a, 'is not between 1 and 10')
except ValueError:
    print(f'{a} is not a positive integer')
Related