Home > Software design >  I want the result of a user input to cause a different variable to be used
I want the result of a user input to cause a different variable to be used

Time:01-11

Disclaimer I'm only 10 days into learning Python (little to no experience in any language prior to this)

I'm having difficulty conceptualizing my problem and thus, a solution for it.

I currently have the following code:

spacer = "_|_"
spacer_btm = " | "
blank = "_"
blank_btm = " "

print(blank   spacer   blank   spacer   blank)
print(blank   spacer   blank   spacer   blank)
print(blank_btm   spacer_btm   blank_btm   spacer_btm   blank_btm)

This is going to print out a grid. Originally i had the blank and blank_btm variables split into 9 separate variables, each denoting a space in the grid that I want to change. The problem I have is that since these are strings, they are immutable. I want to change that blank space/underscore value for something else based on the result of a user input. Ideally the value that replaces the blank or blank_btm would also be a string, but I'm confounded as to what sort of process I should use to get there.

for example if the input is 1 lets say that would mean the top left box in the grid gets "A" (or literally anything as a string)

For reference the previous code I had was

spacer = str("_|_")
spacer_btm = " | "
a1 = str("_")
a2 = str("_")
a3 = str("_")
b4 = str("_")
b5 = str("_")
b6 = str("_")
c7 = str(" ")
c8 = str(" ")
c9 = str(" ")
    
print(a1   spacer   a2   spacer   a3)
print(b4   spacer   b5   spacer   b6)
print(c7   spacer_btm   c8   spacer_btm   c9)

CodePudding user response:

I liked the answer @misterwtf gave but I'll just move a step ahead and make an assumption that you would want the grid to be fixed and would want to fill the values in the boxes, like in tic-tac-toe.

If you have studied about python dictionary, I'd suggest you to use one.

You can simply define the dict variable like:

board = {'9': ' ' , '8': ' ' , '7': ' ' ,
            '6': ' ' , '5': ' ' , '4': ' ' ,
            '3': ' ' , '2': ' ' , '1': ' ' }

and then make the grid like:

print(' '   board['9']   ' |'   ' '   board['8']   ' |'   ' '   board['7'])
print('---|---|---')
print(' '   board['6']   ' |'   ' '   board['5']   ' |'   ' '   board['4'])
print('---|---|---')
print(' '   board['3']   ' |'   ' '   board['2']   ' |'   ' '   board['1'])

It would be better if you make this^ into a function (study about that as well).

Now, you can take the input from the user. Say, you want to fill the top-left box. You'll simply write:

board['9'] = input()

and now when you make the grid again, voila! you've successfully entered the value in the grid.

CodePudding user response:

The closest solution to your code I can think of is the following one.

spacer = "_|_"
spacer_btm = " | "
blank = "_"
blank_btm = " "

print(("A" if input() == "1" else blank)   spacer   blank   spacer   blank)
print(blank   spacer   blank   spacer   blank)
print(blank_btm   spacer_btm   blank_btm   spacer_btm   blank_btm)

When Python executes the input builtin function, the program hangs until you press ENTER.

I would not do it like this but I don't want to give you a solution you would not understand. For example, I suppose you need to prompt the user repeatedly, in a read-eval-print loop, but do you know what a while loop is?

  •  Tags:  
  • Related