I'm trying to create a python cards game (a Brazilian cards game). But when I try to print some values (the cards played) it returns the following error:

Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
play()
File "C:\Documents and Settings\Terminal\Desktop\", line 71, in play
print "You played %a. The computer played %b and %c" % (player_card1, computer_card1, computer_card2)
ValueError: unsupported format character 'a' (0x61) at index 12

player_card1, computer_card1, computer_card2 are numbers from a list...

example:

from random import choice as rc
cards = [1, 2, ...]

player = []

player.append(rc(cards))
player.append(rc(cards))
player.append(rc(cards))

player_card1 = player[0]

Dani AI

Generated

The traceback is coming from the old-style % formatter: when Python sees % it expects a valid conversion character next, and %a is not one of them, so the parser raises ValueError: unsupported format character 'a' (the "index 12" in the message points to the character position inside your format string). and were on the right track — the problem is the format string, not the values in your player list.

A simple, readable fix is to switch to the newer str.format() or (on modern Python) f-strings. These avoid the single-letter % codes and are easier to read:

print("You played {}. The computer played {} and {}".format(
    player_card1, computer_card1, computer_card2))

or (Python 3.6+)

print(f"You played {player_card1}. The computer played {computer_card1} and {computer_card2}")

For quick debugging, print the representation and the type of each variable so you can confirm they really are the card values you expect:

print(repr(player_card1), type(player_card1))

A few quick notes: if you do keep using % formatting, use a supported specifier (for example %s for a generic, safe conversion) and use %% when you want a literal percent sign. If you see odd errors later with %c, remember that %c expects a single character (or an integer code point), so it can behave differently than %s. If you paste a minimal, complete snippet of your play() function I can point out the exact line to change.

Recommended Answers

All 5 Replies

Use lowercase for your string formatting expression.

>>> print 'Hi this is a %s' % 'test'
Hi this is a test
>>> print 'Hi this is a %S' % 'test'
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
ValueError: unsupported format character 'S' (0x53) at index 14
>>>

I did so.

print "You played %a. The computer played %b and %c" % (player_card1, computer_card1, computer_card2)

What you want with %a format. You should have %s for strings, %f for floats, %i for integers %x for hexadecimal.

I never heard about %a , %b or %c formatting.

Try.
print "You played %s. The computer played %s and %s" % (player_card1, computer_card1, computer_card2)

You can not use a% b% c%
http://docs.python.org/library/stdtypes.html#string-formatting-operations

Most commen formatting expression.
%s == String (converts any Python object using str()).
%d == Signed integer decimal
%f == Floating point decimal format.

tonyjv and snippsat:

I've never heard about that. I'm just beginning with python coding, heh
thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.