-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.

Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
  • 1
    You are choosing a random string, printing it, then choosing **another** string and compare it to "1"! – Jacques Gaudin Apr 04 '19 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. – Life is complex Apr 04 '19 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. – Life is complex Apr 04 '19 at 17:59
  • P.S. Why are you using random to accomplish this task? – Life is complex Apr 04 '19 at 18:06

2 Answers2

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)
Jacques Gaudin
  • 15,779
  • 10
  • 54
  • 75
-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.

ralawar
  • 1
  • 2