I'm pretty new to Python and currently trying to create a basic blackjack game using ASCII graphics to represent the cards. I've placed the card images in a list of multiline strings and the idea is to call the specific index for each one when a card needs to be displayed.
However I can't get them to print next to each other, and no amount of tinkering seems to get it to work. Here's the code:
CARDS = ['''
-------
|K |
| |
| |
| |
| K|
------- ''', '''
-------
|Q |
| |
| |
| |
| Q|
------- ''', '''
-------
|J |
| |
| |
| |
| J|
------- ''', '''
-------
|10 |
| |
| |
| |
| 10|
------- ''', '''
-------
|9 |
| |
| |
| |
| 9|
------- ''', '''
-------
|8 |
| |
| |
| |
| 8|
------- ''', '''
-------
|7 |
| |
| |
| |
| 7|
------- ''', '''
-------
|6 |
| |
| |
| |
| 6|
------- ''', '''
-------
|5 |
| |
| |
| |
| 5|
------- ''', '''
-------
|6 |
| |
| |
| |
| 6|
------- ''', '''
-------
|5 |
| |
| |
| |
| 5|
------- ''', '''
-------
|4 |
| |
| |
| |
| 4|
------- ''', '''
-------
|3 |
| |
| |
| |
| 3|
------- ''', '''
-------
|2 |
| |
| |
| |
| 2|
------- ''', '''
-------
|A |
| |
| |
| |
| A|
------- '''
]
BLANKCARD = '''
-------
|XXXXXXX|
|XXXXXXX|
|XXXXXXX|
|XXXXXXX|
|XXXXXXX|
------- '''
def displayCards():
print(CARDS[2] + CARDS[14], end='')
displayCards()
The above code prints the following:
-------
|J |
| |
| |
| |
| J|
-------
-------
|A |
| |
| |
| |
| A|
-------
I've tried using end='' to get rid of the new line, but no joy. Does anyone have any suggestions about how I can get the cards next to each other?
Thanks in advance!
Interesting little problem. Here's a quick solution that I whipped up.
class Card:
def topchar(char):
return '|{} |'.format(char)
def botchar(char):
return '| {}|'.format(char)
def print(char_list):
top = ' ------- '
side ='| |'
topout = ''
topchar = ''
botchar = ''
blankside = ''
for char in char_list:
topout += top + ' '
topchar += Card.topchar(char) + ' '
blankside += side + ' '
botchar += Card.botchar(char) + ' '
print(topout)
print(topchar)
print(blankside)
print(blankside)
print(blankside)
print(botchar)
print(topout)
With the way you're doing it, that would be very difficult. When you write
end=''
That only gets rid of the newline at the very end of the printed text. The problem is, every one of your cards has a new line on the right side:
-------
|J |
| |
| | # <--- Newline here
| |
| J|
-------
You would need to create a function that takes a list of cards, and creates one long line of them. That will be a solid project in its own. You would have to take the strings that make up the cards, cut them into lines (cut on each new line), place all the corresponding lines together, getting rid of the newlines in between, then glue all the pieces together. I might actually try this for a morning project now.
And instead of hard coding all the cards like you have, create a function that you give a card value to, and it creates a card for you.
I suggest you write a function that, given i and n, returns a string which represents line i of card n. You can then call that in a double nested loop, printing the results in sequence, to get the result you need.
You can start by making an example of the output you want to see, to use as a reference while coding the loop.
Related
I am writing a script to select a region in a ComboBox. I can use app.dialog['Region:ComboBox'].select(index), but not app.dialog['Region:ComboBox'].select('string'). I notice the ComboBox is custom and is generated real time. How can I select the proper option using a string? The string would be a region like US West, US East, etc.
| | GroupBox - 'Preferences' (L811, T456, R1108, B593)
| | ['PreferencesGroupBox', 'Preferences', 'GroupBox', 'GroupBox0', 'GroupBox1']
| | child_window(title="Preferences", control_type="Group")
| | |
| | | Static - 'PREFERENCES' (L817, T462, R1102, B476)
| | | ['PREFERENCES', 'PREFERENCESStatic', 'Static25']
| | | child_window(title="PREFERENCES", control_type="Text")
| | |
| | | Static - 'Region Text:' (L0, T0, R0, B0)
| | | ['Region Text:Static', 'Region Text:', 'Static26']
| | | child_window(title="Region Text:", control_type="Text")
| | |
| | | Static - '' (L0, T0, R0, B0)
| | | ['Static27']
| | |
| | | Static - 'Region:' (L822, T498, R937, B512)
| | | ['Region:Static', 'Region:', 'Static28']
| | | child_window(title="Region:", control_type="Text")
| | |
| | | Custom - '' (L947, T492, R1097, B518)
| | | ['Custom3', 'Region:Custom']
| | | |
| | | | ComboBox - '' (L947, T492, R1097, B518)
| | | | ['ComboBox', 'Region:ComboBox', 'ComboBoxESRI.ArcGIS.Azure.IaaS.Interfaces.RegionInfo']
| | | | |
| | | | | Edit - '' (L0, T0, R0, B0)
| | | | | ['Edit', 'Edit0', 'Edit1']
| | | | | child_window(auto_id="PART_EditableTextBox", control_type="Edit")
| | |
| | | Static - 'Remote Desktop Port:' (L822, T534, R937, B548)
| | | ['Remote Desktop Port:', 'Remote Desktop Port:Static', 'Static29']
| | | child_window(title="Remote Desktop Port:", control_type="Text")
| | |
| | | Edit - '3389' (L947, T528, R1097, B554)
| | | ['Edit2', 'Remote Desktop Port:Edit']
| | | child_window(title="3389", control_type="Edit")
| | | |
| | | | ScrollBar - '' (L0, T0, R0, B0)
| | | | ['ScrollBar', 'ScrollBar0', 'ScrollBar1']
| | | | child_window(auto_id="VerticalScrollBar", control_type="ScrollBar")
| | | |
| | | | ScrollBar - '' (L0, T0, R0, B0)
| | | | ['ScrollBar2']
| | | | child_window(auto_id="HorizontalScrollBar", control_type="ScrollBar")
| | | |
| | | | Button - 'r' (L0, T0, R0, B0)
| | | | ['r', 'Button8', 'rButton']
| | | | child_window(title="r", auto_id="PART_ClearText", control_type="Button")
| | |
| | | CheckBox - 'Track application usage anonymously' (L822, T564, R1097, B582)
| | | ['Track application usage anonymously', 'CheckBox', 'Track application usage anonymouslyCheckBox', 'Track application usage anonymously0', 'Track application usage anonymously1']
| | | child_window(title="Track application usage anonymously", control_type="CheckBox")
| | | |
| | | | Static - 'Track application usage anonymously' (L846, T565, R1043, B581)
| | | | ['Track application usage anonymously2', 'Track application usage anonymouslyStatic', 'Static30']
| | | | child_window(title="Track application usage anonymously", control_type="Text")
Here's how I implemented it. This function emulates the select() function more efficiently.
def comboselect(combo,sel):
combo.type_keys("{ENTER}") # Selects the combo box
texts = combo.texts() #gets all texts available in combo box
try:
index = texts.index(str(sel)) #find index of required selection
except ValueError:
return False
sel_index = combo.selected_index() # find current index of combo
if(index>sel_index):
combo.type_keys("{DOWN}"*abs(index-sel_index))
else:
combo.type_keys("{UP}"*abs(index-sel_index))
return True
I'm trying to parallelize some heavier computations like so:
inputs = (p | "Read" >> beam.io.ReadFromAvro('/mypath/myavrofiles*')
| "Generate Key" >> beam.Map(lambda row: (gen_key(row), row)))
calc1_results = inputs | "perform calc1" >> beam.Pardo(Calc1())
calc2_results = inputs | "perform calc2" >> beam.Pardo(Calc2())
combined = (({"calc1": calc1_results, "calc2": calc2_results})
| beam.CoGroupByKey()
| beam.Values())
final = combined | "Use Grouped results" >> beam.ParDo(PerformFinalCalculation())
Each heavy calc emits (key, result)
Each key is unique for each input. One input, One result, one Key
Is there some way to emit from the CoGroupByKey after a single result1/result2 has been collected for each key?
Ultimately I'd like to achieve something along the lines of:
+------------+
| |
| Input |
| +-----------------+
+------------+ |
| |
v-------------------v |
+------------+ +------------+ |
| | | | |
| Heavy | | Heavy | |
| Calc 1 | | Calc 2 | |
| | | | |
+------------+ +------------+ |
| | |
| | |
| | |
+--v------------v--+ |
| Merged | |
| original dict, +<---------------+
|result 1, result2 |
| |
+------------------+
Imagine that I have the follow in a .txt file:
+---+-----+-----+
| | | |
| | | |
| | +=====+
+---+ | | |
| | | | |
| +-+ +=====+
| | | | |
| | +-+ | |
| | | | |
| | +-+ |
| | |_____|
| | | |
+---+-----+-----+
What is the best way I can turn this into a 2D list that also includes the whitespace?
I wrote a function that will print it as a string.. But I simply do not know how to make this thing work. I'm just stumped.
You can use the .readlines() function, like so:
with open('in.txt') as input_file:
data = input_file.readlines()
assert data[0][0] == '+'
assert data[3][4] == '|'
with open('filename.txt', 'r') as f:
lines = [[letter for letter in line] for line in f.read().split('\n')]
This question already has answers here:
"TypeError: argument of type 'NoneType' is not iterable"?
(3 answers)
Closed 8 years ago.
i made this hangman program but it is giving 'nonetype' error whenever i run it
program running-
Whenever I enter a word the output is like this
Hang Man Game
Guess a word
a
-----
You gussed one word correctly
-----a---------------
Traceback (most recent call last):
File "F:/coding/python/python programming for absolute beginners/chapter 5/Hang Man Game.py", line 129, in <module>
if guess in used:
TypeError: argument of type 'NoneType' is not iterable
The code:
print("\t\t\tHang Man Game")
import random
set=("happy","australia","punjab","criclet","tennis")
choose=random.choice(set)
correct=choose
HANGMAN=('''
_______
|
|
|
|
|
|
|
|
|
|
|
___
''',
'''
______________
| |
|
|
|
|
|
|
|
|
|
___
''',
'''
______________
| |
| O
|
|
|
|
|
|
|
|
___
''',
'''
______________
| |
| O
| |
| |
| |
| |
|
|
|
|
___
''',
'''
______________
| |
| O
| |
| ---|
| |
| |
|
|
|
|
___
''',
'''
______________
| |
| O
| |
| ---|---
| |
| |
|
|
|
|
___
''',
'''
______________
| |
| O
| |
| ---|---
| |
| |
| /
| /
| /
|
___
''',
'''
______________
| |
| O
| |
| ---|---
| |
| |
| / \
| / \
| / \
|
___
'''
)
MAX_WRONG=(len(HANGMAN)-1)
wrong=0
new=""
used=[]
so_far="-"*len(correct)
guess=raw_input("Guess a word\n")
while(so_far!=correct and wrong<MAX_WRONG):
print(so_far)
if guess in used:
print("you have already used it")
else:
if guess in correct:
print("You gussed one word correctly\n")
used.append(guess)
for i in range(len(correct)):
if guess==correct[i]:
new=new+guess
else:
new=new+so_far
so_far=new
else:
used=used.append(guess)
wrong=wrong+1
You cannot do this
used = used.append(guess)
The append function returns None, it modifies used in place. So you are basically appending guess on the right hand side, but then assigning None to used. So the next iteration of the while loop you are trying to iterate over None, which is what the error is telling you.
You simply need to say
used.append(guess)
Or
used += [guess]
The member function append of list modifies the list in place and does not return anything.
At the first iteration, used is an empty list, but when you reach the line used=used.append(guess), used is first modified by append as you expect it to be, and then you assign the return value of append (which is None) to used. Thus, in the second iteration, used is None.
Simply change the two lines using append for used.append(guess).
There are other problems with your code though, one of them being that you ask for a guess only once (you can put the guess = raw_input("Guess a word\n") in the loop instead).
s = [0,2,6,4,7,1,5,3]
def row_top():
print("|--|--|--|--|--|--|--|--|")
def cell_left():
print("| ", end = "")
def solution(s):
for i in range(8):
row(s[i])
def cell_data(isQ):
if isQ:
print("X", end = "")
return ()
else:
print(" ", end = "")
def row_data(c):
for i in range(9):
cell_left()
cell_data(i == c)
def row(c):
row_top()
row_data(c)
print("\n")
solution(s)
My output has a space every two lines, when there shouldn't be, I'm not sure where it's creating that extra line.
The output is suppose to look like this:
|--|--|--|--|--|--|--|--|
| | | | | | X| | |
|--|--|--|--|--|--|--|--|
| | | X| | | | | |
|--|--|--|--|--|--|--|--|
| | | | | X| | | |
|--|--|--|--|--|--|--|--|
| | | | | | | | X|
|--|--|--|--|--|--|--|--|
| X| | | | | | | |
|--|--|--|--|--|--|--|--|
| | | | X| | | | |
|--|--|--|--|--|--|--|--|
| | X| | | | | | |
|--|--|--|--|--|--|--|--|
| | | | | | | X| |
|--|--|--|--|--|--|--|--|
I know this chess board isn't very square but this is only a rough draft at the moment.
Here is an alternative implementation:
def make_row(rowdata, col, empty, full):
items = [col] * (2*len(rowdata) + 1)
items[1::2] = (full if d else empty for d in rowdata)
return ''.join(items)
def make_board(queens, col="|", row="---", empty=" ", full=" X "):
size = len(queens)
bar = make_row(queens, col, row, row)
board = [bar] * (2*size + 1)
board[1::2] = (make_row([i==q for i in range(size)], col, empty, full) for q in queens)
return '\n'.join(board)
queens = [0,2,6,4,7,1,5,3]
print(make_board(queens))
which results in
|---|---|---|---|---|---|---|---|
| X | | | | | | | |
|---|---|---|---|---|---|---|---|
| | | X | | | | | |
|---|---|---|---|---|---|---|---|
| | | | | | | X | |
|---|---|---|---|---|---|---|---|
| | | | | X | | | |
|---|---|---|---|---|---|---|---|
| | | | | | | | X |
|---|---|---|---|---|---|---|---|
| | X | | | | | | |
|---|---|---|---|---|---|---|---|
| | | | | | X | | |
|---|---|---|---|---|---|---|---|
| | | | X | | | | |
|---|---|---|---|---|---|---|---|
It is now very easy to change the width of the board by changing the strings passed to row, empty, full; I added an extra char to each, resulting in a (somewhat) squarer board.
You are still printing an extra newline:
def row(c):
row_top()
row_data(c)
print("\n")
Remove the explicit ''\n'` character:
def row(c):
row_top()
row_data(c)
print()
or better still, follow my previous answer more closely and print a closing | bar:
def row(c):
row_top()
row_data(c)
print('|')