5

Environment

I am using Python 3 and my OS is Windows 7. I understand that some commands have changed from the transition from python 2.7 to 3 (What I have used).

Problem

The variable is temporary but is this:

(((((0, 7), 7), 8), 4), 5) 

Here is the code to get rid of the brackets:

randy = randy.replace(")", "")
randy = randy.replace("(", "")
randy = randy.replace(" ", "")

When it tries to execute the replace function, I get thrown an error:

Traceback (most recent call last):
File "<string>", line 248, in run_nodebug
File "Criptic.py", line 134, in <module>
randy = randy.replace(")", "")
AttributeError: 'tuple' object has no attribute 'replace'

Edit:

Here is all of the code:

#Import string
import string
import random
#Input user data
text = input("Enter your text to be converted: ")

#Test Print
print("-------------------------")
print("Your text is: ",text)




#Break up all the data
data = list(text)

#Lowercase
count = text.count("a")
count1 = text.count("b")
count2 = text.count("c")
count3 = text.count("d")
count4 = text.count("e")
count5 = text.count("f")
count6 = text.count("g")
count7 = text.count("h")
count8 = text.count("i")
count9 = text.count("j")
count10 = text.count("k")
count11 = text.count("l")
count12 = text.count("m")
count13 = text.count("n")
count14 = text.count("o")
count15 = text.count("p")
count16 = text.count("q")
count17 = text.count("r")
count18 = text.count("s")
count19 = text.count("t")
count20 = text.count("u")
count21 = text.count("v")
count22 = text.count("w")
count23 = text.count("x")
count24 = text.count("y")
count25 = text.count("z")
count26 = text.count("A")

#Uppercase
count27 = text.count("B")
count28 = text.count("C")
count29 = text.count("D")
count30 = text.count("E")
count31 = text.count("F")
count32 = text.count("G")
count33 = text.count("H")
count34 = text.count("I")
count35 = text.count("J")
count36 = text.count("K")
count37 = text.count("L")
count38 = text.count("M")
count39 = text.count("N")
count40 = text.count("O")
count41 = text.count("P")
count42 = text.count("Q")
count43 = text.count("R")
count44 = text.count("S")
count45 = text.count("T")
count46 = text.count("U")
count47 = text.count("V")
count48 = text.count("W")
count49 = text.count("X")
count50 = text.count("Y")
count51 = text.count("Z")

#Other Characters
count52 = text.count(" ")
count53 = text.count("?")
count54 = text.count("@")
count55 = text.count("(")
count56 = text.count(")")
count57 = text.count(".")

#Numbers
count58 = text.count("1")
count59 = text.count("2")
count60 = text.count("3")
count61 = text.count("4")
count62 = text.count("5")
count63 = text.count("6")
count64 = text.count("7")
count65 = text.count("8")
count66 = text.count("9")
count67 = text.count("0")

#Counting how many characters in the sentence
finalcount = count + count1 + count2 + count3 + count4 + count5 + count6 + count7 + count8 + count9  + count10  +   count11  + count12  + count13  + count14 + count15 + count16 + count17 + count18 + count19 + count20 + count21 + count22 + count23 + count24 + count25 + count26 + count27 + count28 + count29 + count31 + count32 + count33 + count34 + count35 + count36 + count37 + count38 + count39 + count40 + count41 + count42 + count43 + count44 + count45 + count46 + count47 + count48 + count49 + count50 + count51 + count52 + count53 + count54 + count55 + count56 + count57 + count58 + count59 + count60 + count61 + count62 + count63 + count64 + count65 + count66 + count67

#Final count of Characters
print("       Chars: ",finalcount)
print("-------------------------")





char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()?_+-=1234567890           "
charnum = 74 + 11
list(char)

randy = 0
num = 0
while num < finalcount:
    rand = random.randrange(1,9)
    randy = randy,rand
    finalcount = finalcount - 1
    if rand == 1:
    print(text[finalcount],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],",")
if rand == 2:
    print(char[random.randrange(0,charnum)],text[finalcount],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],",")
if rand == 3:
    print(char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],text[finalcount],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],",")
if rand == 4:
    print(char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],text[finalcount],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],",")
if rand == 5:
    print(char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],text[finalcount],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],",")
if rand == 6:
    print(char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],text[finalcount],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],",")
if rand == 7:
    print(char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],text[finalcount],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],",")
if rand == 8:
    print(char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],text[finalcount],char[random.randrange(0,charnum)],",")
if rand == 9:
   print(char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],text[finalcount],",")


randy = randy.replace(")", "")
randy = randy.replace("(", "")
randy = randy.replace(" ", "")

print("-------------------------")
print("::::::::Completed::::::::")
print("-------------------------")
print(randy)
print("-This is your code unlock key. Without this code it will not decript!!!! ")
print("-------------------------")
6
  • Only strings have a replace method. Maybe you have to define your ((((...) thingy as string? Tuple: randy = (((((0, 7), 7), 8), 4), 5), string: randy = "(((((0, 7), 7), 8), 4), 5)" Commented Feb 24, 2011 at 1:58
  • @James Dyson: Strings are defined like in most other languages. With double or single quotes. Commented Feb 24, 2011 at 2:01
  • But I am not setting the varaible.....The program does Commented Feb 24, 2011 at 2:13
  • 1
    The program only does what you tell it. And you set the variable, here: ` rand = random.randrange(1,9) randy = randy,rand`. Commented Feb 24, 2011 at 4:42
  • (Your problems aren't Python 3 specific, btw, so I added a plain Python tag). Commented Feb 24, 2011 at 4:44

4 Answers 4

8

You have a tuple (a sort of list) of numbers, and you want to make that a string. You can't replace the brackets, they aren't a part of the variable, they are just a part of its representation.

Instead you should use join() and str()

 result = " ".join(str(x) for x in randy)

However, this probably will not give the right result either, as it is a nested list of tuples. You probably mean: randy = randy + (rand,) instead if randy = randy,rand.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much...But this came up: Traceback (most recent call last): File "E:\Home\AISS\Criptic.py", line 112, in <module> randy = randy + (rand,) TypeError: can only concatenate list (not "tuple") to list
@James Dyson: The way to say "Thank you" on Stackoverflow is to click the up arrow to the left of the answers you found helpful. :-) Click the Checkbox on the answer you found most helpful to select it as the "most correct" answer. That way other people with the same problem as you can learn too.
@James Dyson: Ah. So it's not a tuple, it's a list. In that case you do randy.append(rand). You also need to make sure that randy is a list from the beginning like this: randy = [0].
Oh, right, you need points even to upvote. I forgot that. OK, ignore it. :)
2
randy = randy,rand

This notation creates a tuple of two elements, tuples do not have a method called replace.

If you just want to concatenate the numbers into a list representation separated by commas, you could do this in your loop:

randy = []
num = 0
while num < finalcount:
    rand = random.randrange(1,9)
    randy.append(rand)

Then you remove the replace lines, and instead do this

randy = ",".join(randy)

to get a string that has the values separated by commas.

You should maybe add the output that you would like to get to your question, because at the moment it's not quite clear what you want to do exactly.

1 Comment

The output I would like to get is this: 4,5,3,5,2,5 (Example)
1
exam_st_date = (11, 12, 2014)
# Sample Output : The examination will start from : 11 / 12 / 2014

newtup = str(exam_st_date).replace(',', ' /')


print(newtup)

look at mine example should give you some results

Comments

-2

To delete a list of characters, you could use the string's translate method:

import string
name = "(((((0, 7), 7), 8), 4), 5)"
table = string.maketrans( '', '', )
print name.translate(table,"()")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.