Here is one function from my code
def seq():
q=1
n=rann()
List=[n]
while q<n:
if (n % 2):
n = 3*n 1
List.append(n)
else:
n=n//2
List.append(n)
if (len(List)>=len(y)):
print(List)
else:
return(rann())
x=input("enter name") #the next four lines of the code are my first trial.
y= [char for char in x]
for i in y:
y[i]=rann()
seq()
I have two functions, the 1st function, rann(), generates an integer and the other(the code above) generates a sequence based on that number. I wanted to take an input x and replace every character with the sequence generated. I tried doing this
text = 'bat ball'
y=[char for char in text]
y[0:] = rann()
print(y)
For example let the input be "bat ball" so what I wanted was to replace every character of the input by every term of the sequence. I know there is a lot of things wrong But I can't figure out another method.
CodePudding user response:
You mean like this? I contrived a rann(x)
to generate the ascii code value of the character x
, but you get the idea.
# example rann()
def rann(x):
return [ord(ch) for ch in x]
def seq(n):
q = 1
List = [n]
while q < n:
if n % 2:
n = 3*n 1
List.append(n)
else:
n = n // 2
List.append(n)
if len(List) >= len(y):
print(List)
else:
return n
text = 'bat ball'
x = [char for char in text]
y = rann(x)
print(y)
[seq(n) for n in y]
CodePudding user response:
It would be useful to see what kind of integer or sequence you are trying to generate here- please post an example.
From what I can understand, you want to generate a list of numbers based on the input string?
the easiest way may be to use the ascii code for each char like this:
def seq():
'''
(None) -> List of int
Get user input and convert to int list
'''
nums = []
text = input("Enter text: ")
for ch in text:
nums.append(ord(ch))
return nums
It is easier to just build a new list from the int values than to mutate a list that you are iterating through.
If rann generates a list of int from the char, replace ord above with rann and have it return a list of int.
A sample of you expected output would really help here.