-1

Please help me. I am a noob and the code does not run. The input should be a number which indicates the index where the 0 turns to an 1.

board2 =[0,0,0,0,0,0,0,0,0]
inp = input('Input Number 0-8:')
if inp == int():
    a = inp
    for i in board2:
        board2.replace(i[a],1)
        return board2

  • 2
    how can you iterate through `int()` – BuddyBob May 11 '21 at 16:45
  • 1
    You say "the code does not run." Please add the error message you get. – Sarah Messer May 11 '21 at 16:46
  • 1
    There are multiple things wrong with this code, well *before* hitting the point where ``'list' object has no attribute 'replace'``. Did you go through [the python.org tutorial](https://docs.python.org/3/tutorial/index.html) already? – MisterMiyagi May 11 '21 at 16:48
  • `boad2[3] = 10` will replace the fourth element of `board2` with `10`. Not sure that is enough to fix the above. – norok2 May 11 '21 at 16:50

3 Answers3

1

You can check if the value input is both an int and if it is in range that you described.

Then you can use the index to replace the value in board2 to 1.

Also, the input() function defaults to string.

board2 =[0,0,0,0,0,0,0,0,0]
inp = input('Input Number 0-8:')
if 0 <= int(inp) < len(board2):
    board2[int(inp)] = 1
print(board2)
    
Joe Thor
  • 510
  • 3
  • 10
  • 1
    Don't use `range` for a bound check, thats very inefficient. Do `if 0 <= int(inp) < len(board2):` – flakes May 11 '21 at 16:58
  • Thank you! Updated my answer. – Joe Thor May 11 '21 at 17:36
  • @flakes Using `range` for checking bounds is perfectly fine, see [this](https://stackoverflow.com/questions/30081275/why-is-1000000000000000-in-range1000000000000001-so-fast-in-python-3). – Asocia May 11 '21 at 18:20
  • 1
    @Asocia true its not as bad as it could be, but it's still about 4x slower than the `if` variant. There's a lot of extra objects allocated for that expense: https://gist.github.com/cal-pratt/1e8c752751e50fa63ca7610547ee9793 – flakes May 11 '21 at 18:29
  • @flakes Yes, you are right. Actually most of the time is spent with the function call. If you define the range above the function like `my_range = range(250, 750)` and then use it inside the `range_check` its performance is not that bad. I wouldn't mind using it if it's a few nanoseconds slower than the `if` check. Thanks for clarifying it though. – Asocia May 11 '21 at 18:39
  • 2
    @Asocia Yeah, at that point you're doing more work to use range and are losing the initially intended readability advantage (although IMHO the double bounded `if` is generally a more readable/ Pythonic way to write this in the first place). – flakes May 11 '21 at 18:43
0

Almost there! you don't really need to use "replace" here; you can just modify the element which resides at the index provided directly. The only criteria you would need to consider is that the number is greater than zero AND within the range of the "board2" list provided.

board2 =[0,0,0,0,0,0,0,0,0]
inp = input('Input Number 0-8:')
if (0 < int(inp) < len(board2)):
    board2[int(inp)] = 1
    print(*board2) 
Shawn Ramirez
  • 323
  • 1
  • 7
0

This code snippet should solve your question:

board2 = [0,0,0,0,0,0,0,0,0]

index = int(input('Input Number 0-8:'))

if index in range(len(board2)):
    board2[index] = 1

print(board2)
SneakyTurtle
  • 1,634
  • 1
  • 9
  • 17
gikonyo
  • 1
  • 1