-1

I am trying to write a Suduoku game. Every time I use a number, I need to delete it from the list.

I have already tried .pop[1], but it did nothing when the number was one.

import random rownum1 = ["1","2","3","4","5","6","7","8","9"] print(random.choice(rownum1)) if random.choice(rownum1) == "1": del rownum1[0] print(rownum1) 

If one is randomly selected I expect the output to be:

["2","3","4","5","6","7","8","9"] 

But nothing is happening when one is randomly chose.

4
  • 1
    You are choosing a random string, printing it, then choosing another string and compare it to "1"! Commented Apr 4, 2019 at 17:42
  • random.choice(rownum1) produces a single value based on random, so selecting the correct number (e.g., 1) with random is hard. Commented Apr 4, 2019 at 17:48
  • I put random.choice(rownum1) in a while loop for 3 rounds and this was the output: pass one: 4, 8, 2, 9, 8, 1, 4, 4, 1 -- pass two: 5, 5, 6, 5, 7, 2, 1, 2, 7 -- pass three: 6, 6, 4, 9, 9, 2, 4, 9, 7 -- your code requires some reengineering to use random. Commented Apr 4, 2019 at 17:59
  • P.S. Why are you using random to accomplish this task? Commented Apr 4, 2019 at 18:06

2 Answers 2

0

You are choosing a random string, printing it, then choosing another string and compare it to "1"!

Each time you call random.choice(rownum1), you will get a new random string. The correct way to do what you want is to store the random string in a variable:

import random rownum1 = ["1","2","3","4","5","6","7","8","9"] num = random.choice(rownum1) # <- store the value in a variable for later reuse print(num) if num == "1": del rownum1[0] print(rownum1) 
Sign up to request clarification or add additional context in comments.

Comments

-1

The problem with doing .pop[1] on your particular list is that the items in your list are strings, whereas 1 is an integer. Thus, you must do .pop("1") to remove the "1" from your list.

2 Comments

list.pop() (note the parantheses) takes as input an index to delete, it is not like list.remove()
I tried this but i get the error: 'builtin_function_or_method' object is not subscriptable

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.