Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3305750
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:12:24+00:00 2026-05-17T21:12:24+00:00

I am trying to make a simple text-based game in Python. The idea of

  • 0

I am trying to make a simple text-based game in Python. The idea of which revolves around a deck of playing cards, representing the card face values with A,2,3,4,5,6,7,8,9,10,J,Q, and K (Joker is not used). The suit of the cards will be represented by s,d,h, and c. As an example, “Ace of Hearts” would be represented as Ah.

I am using two random.choice() functions to choose a random face value and a random suit, with 10 being represented by ‘X’. This is where I encounter my problem. Ideally, I would like to change ‘X’ when it appears to ’10’. Here is my “test” code for generating a random card:

import random

firstvar = random.choice('A23456789XJQK')
secondvar = random.choice('sdhc')
newvar = firstvar + secondvar
if newvar[0] == 'X':
    newvar[0] = '10'
    print newvar

else:
    print newvar

I quickly found out that this causes an error.

TypeError: ‘str’ object does not support item assignment

In short, my question is: “How would I go about changing ‘X’ in my program to ’10’?

Thanks for any help you can offer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:12:24+00:00Added an answer on May 17, 2026 at 9:12 pm

    Change it before you concatenate.

    firstvar = random.choice('A23456789XJQK') 
    secondvar = random.choice('sdhc') 
    newvar = (firstvar + secondvar) if firstvar != 'X' else ('10' + secondvar)
    

    Alternatively, don’t use ‘X’ to begin with:

    firstvar = random.choice(['A', '2', '3', ..., '10', ...])
    

    Another alternative would be the replace method on strings:

    newvar = firstvar.replace('X', '10') + secondvar
    

    The second method is probably best for this situation unless you need to use ‘X’ elsewhere in your code and that can’t be changed. It is worth looking at other methods for dealing with immutable strings though because the second isn’t always appropriate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to make simple minesweeper game in python, but have one problem. I have
I'm trying to make a simple text-based progress bar in perl, in order to
I'm trying to make a simple text editor with winapi, its working for simple
I am fairly new to iOS development and trying make a simple app which
I'm trying make my first python app. I want make simple email sender form.
I am trying to make a .bat file that creates a simple text file.
I'm working on a text-based adventure game and I'm trying to externalise as much
I'm trying to make a fairly simple image slideshow to replace a flash based
I am looking to add a simple text based trivia type game within an
I am very new Javascript and am messing around trying to make a simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.