I am attempting to create code where the user can alter the A elements in the list continuously. If another letter in the list is replaced however, it should ask for new input from the user, and go back to the code where the user can alter A elements in the list. My code isn't working however. I am new to coding, so please don't explain things in a complicated manner. I need help!
Here is my code:

list = [['A', 'B', 'A'],['C', 'D', 'B']]
print m
f=input("Index for sublist ")
g=input("Index for sublist item ")

def errordetection ():
    print "Please select another sublist item to replace"
    f=input("Index for sublist ")
    g=input("Index for sublist item ")

while (m[f][g]!='A'):
    errordetection()

if (m[f][g]=='A'):
    m[f][g]='C' 
    print list

Recommended Answers

All 10 Replies

I am attempting to create code where the user can alter the A elements in the list continuously. If another letter in the list is replaced however, it should ask for new input from the user, and go back to the code where the user can alter A elements in the list. My code isn't working however. I am new to coding, so please don't explain things in a complicated manner. I need help!
Here is my code:

list = [['A', 'B', 'A'],['C', 'D', 'B']]
print m
f=input("Index for sublist ")
g=input("Index for sublist item ")

def errordetection ():
    print "Please select another sublist item to replace"
    f=input("Index for sublist ")
    g=input("Index for sublist item ")

while (m[f][g]!='A'):
    errordetection()

if (m[f][g]=='A'):
    m[f][g]='C' 
    print list

list = [ ... ]
should be
m = [...]

Even changing it to this still does not work:

lst = [['A', 'B', 'A'],['C', 'D', 'B']]
print lst
f=input("Index for sublist ")
g=input("Index for sublist item ")

def errordetection ():
    print "Please select another sublist item to replace"
    f=input("Index for sublist ")
    g=input("Index for sublist item ")

while (m[f][g]!='A'):
    errordetection()

if (m[f][g]=='A'):
    m[f][g]='C' 
    print list

You should change 'lst' to 'm' because your lines 11 and 14 are still referring to 'm'.

Don't use list as variable name. What is m, it is not defined before use at line 2?

Ug. How did you manage to get rid of the line numbers and code coloring? Use the (CODE) button next time, please.

There are a lot of problems, but lets start with just three:

  1. What you show can't even be running: It would stop with a problem on the second line: print m So I expect you didn't paste all your code...
  2. You should be using raw_input(thePrompt) not input(thePrompt)
  3. You should not use a list named 'list': It is confusing to both readers and sometimes to the Python interpreter ('list' is a built in type and a conversion function). Name it 'myList' or something more descriptive.

The general rule for new programmers (and wise old ones, too) is to write a very small program that works, then gradually modify it so it does what you want. Start with this program

myList = [['A', 'B', 'A'],['C', 'D', 'B']]
print(myList)

Then add a loop:

myList = [['A', 'B', 'A'],['C', 'D', 'B']]
while True:
    user_input = raw_input("Type 'Q' to quit ")
    if user_input.upper().startswith('Q'):
        break
    print(myList)

And continue making small changes, testing that your program still works, and improving until you are satisfied (or the deadline arrives)

commented: Please be kinder in your responses. +0
commented: No, bad words given, good advice. +2

The code sample written above however,doesn't meet the intention of what I am trying to program. I've changed my list name to 'm' and still experience issues.

m = [['A', 'B', 'A'],['C', 'D', 'B']]
print m
f=input("Index for sublist ")
g=input("Index for sublist item ")

def errordetection ():
    print "Please select another sublist item to replace"
    f=input("Index for sublist ")
    g=input("Index for sublist item ")

while (m[f][g]!='A'):
    errordetection()

if (m[f][g]=='A'):
    m[f][g]='C' 
    print list

because of strange last line and not try except handling. Start small and add functionality with one step and test also it is good to prepare testing material in advance and run the tests after each change (unittest).

Print the values in your program for debugging!

Of course the code sample doesn't do what you want. But it runs and does what you would expect from reading it. Your code, on the other hand also doesn't do what you want, and it isn't obvious what it does from reading it. I'm suggesting that you start with something that

  • Is simple enough to understand easily
  • Doesn't do what you want
  • Can be "mutated" toward what you want

And then mutate it until it does do what you want.

Does your code run? If not, what is the exact error you get (please cut and paste it)... bet before you do that, look at it closely, read what it is saying and try to figure out what it means by that. This is an important skill to learn, and you have to actually think hard sometimes to get it.

If it does run, then what is the problem with how it runs? Say what you wanted to see and what you did see. And think hard about what could be causing it to act that way. Put a print statement somewhere (or several places) so when the program passes through that spot in your code you can see it happening on the screen.

Programming is (among other things) making your understanding of the problem so precise that you can describe how to solve it to an idiot savant that always does exactly what you tell it to do. (or complains that it doesn't understand what you tried to say: A syntax error). You need to be able to state things clearly to yourself at least. If you can do that, it is not so hard to make a clear statement to other people.

I don't understand what your stated problem is: The English language description was confusing to me. I don't understand what you are 'complaining' about your code: Your 'complaint' is not specific enough.

Tony and I have both made the suggestion that you start small and grow the program. Please think about why that seems the wrong thing to you... when two experienced programmers are both saying it is the right thing.

I pointed to three issues (and Tony mentioned two of the same). You have changed your code to partly (there is a remaining 'list' on the last line) fix two of the issues... with one change: Good! But you need to address the rest, too.

I suggest you leave griswolf's code for later and do those three exercises on functions that you had not time to do (yourself).

Then complete this before you start the current assignment:

for number in ('a','b','1','','234234', '-234','4','-1'):
     if check(number):
         if in_correct_range(int(number), low, high):
             print('Good! You gave right size of number')
         else:
             print('The number you gave was outside the limits')
     else:
          print('That was not number')

Here is missing two functions you should define, fixed values for range check. Write those. After that change this loop to while, which only exits the loop when correct input is given. Then put everything in function that returns that correct value out from function as integer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.